fork of indigo with slightly nicer lexgen
at main 1.8 kB view raw
1package syntax 2 3import ( 4 "bufio" 5 "fmt" 6 "os" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10) 11 12func TestInteropAtIdentifiersValid(t *testing.T) { 13 assert := assert.New(t) 14 file, err := os.Open("testdata/atidentifier_syntax_valid.txt") 15 assert.NoError(err) 16 defer file.Close() 17 scanner := bufio.NewScanner(file) 18 for scanner.Scan() { 19 line := scanner.Text() 20 if len(line) == 0 || line[0] == '#' { 21 continue 22 } 23 _, err := ParseAtIdentifier(line) 24 if err != nil { 25 fmt.Println("FAILED, GOOD: " + line) 26 } 27 assert.NoError(err) 28 } 29 assert.NoError(scanner.Err()) 30} 31 32func TestInteropAtIdentifiersInvalid(t *testing.T) { 33 assert := assert.New(t) 34 file, err := os.Open("testdata/atidentifier_syntax_invalid.txt") 35 assert.NoError(err) 36 defer file.Close() 37 scanner := bufio.NewScanner(file) 38 for scanner.Scan() { 39 line := scanner.Text() 40 if len(line) == 0 || line[0] == '#' { 41 continue 42 } 43 _, err := ParseAtIdentifier(line) 44 if err == nil { 45 fmt.Println("FAILED, BAD: " + line) 46 } 47 assert.Error(err) 48 } 49 assert.NoError(scanner.Err()) 50} 51 52func TestDowncase(t *testing.T) { 53 assert := assert.New(t) 54 55 aidh, err := ParseAtIdentifier("example.com") 56 assert.NoError(err) 57 assert.True(aidh.IsHandle()) 58 assert.False(aidh.IsDID()) 59 _, err = aidh.AsHandle() 60 assert.NoError(err) 61 _, err = aidh.AsDID() 62 assert.Error(err) 63 64 aidd, err := ParseAtIdentifier("did:web:example.com") 65 assert.NoError(err) 66 assert.False(aidd.IsHandle()) 67 assert.True(aidd.IsDID()) 68 _, err = aidd.AsHandle() 69 assert.Error(err) 70 _, err = aidd.AsDID() 71 assert.NoError(err) 72} 73 74func TestEmpty(t *testing.T) { 75 assert := assert.New(t) 76 77 atid := AtIdentifier{} 78 79 assert.False(atid.IsHandle()) 80 assert.False(atid.IsDID()) 81 assert.Equal(atid, atid.Normalize()) 82 atid.AsHandle() 83 atid.AsDID() 84 assert.Empty(atid.String()) 85}