diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-18 17:07:09 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-09-18 17:07:09 +0900 |
commit | fe865a812401c2c612f2cd17cedd4728dc4798f7 (patch) | |
tree | 007dbc653364809e2273ba95aef0f1bea2006127 /spec/spec_test.go | |
parent | Update CHANGELOG (diff) | |
download | tre-fe865a812401c2c612f2cd17cedd4728dc4798f7.tar.gz tre-fe865a812401c2c612f2cd17cedd4728dc4798f7.tar.xz |
Generate constant values representing mode IDs, mode names, kind IDs, and kind names
Diffstat (limited to 'spec/spec_test.go')
-rw-r--r-- | spec/spec_test.go | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/spec/spec_test.go b/spec/spec_test.go new file mode 100644 index 0000000..54d7b7d --- /dev/null +++ b/spec/spec_test.go @@ -0,0 +1,211 @@ +package spec + +import ( + "fmt" + "testing" +) + +var idTests = []struct { + id string + invalid bool +}{ + { + id: "foo", + }, + { + id: "foo2", + }, + { + id: "foo_bar_baz", + }, + { + id: "f_o_o", + }, + { + id: "2foo", + invalid: true, + }, + { + id: "_foo", + invalid: true, + }, + { + id: "foo_", + invalid: true, + }, + { + id: "foo__bar", + invalid: true, + }, +} + +func TestValidateIdentifier(t *testing.T) { + for _, tt := range idTests { + t.Run(tt.id, func(t *testing.T) { + err := validateIdentifier(tt.id) + if tt.invalid { + if err == nil { + t.Errorf("expected error didn't occur") + } + } else { + if err != nil { + t.Errorf("unexpected error occurred: %v", err) + } + } + }) + } +} + +func TestLexKindName_validate(t *testing.T) { + for _, tt := range idTests { + t.Run(tt.id, func(t *testing.T) { + err := LexKindName(tt.id).validate() + if tt.invalid { + if err == nil { + t.Errorf("expected error didn't occur") + } + } else { + if err != nil { + t.Errorf("unexpected error occurred: %v", err) + } + } + }) + } +} + +func TestLexModeName_validate(t *testing.T) { + for _, tt := range idTests { + t.Run(tt.id, func(t *testing.T) { + err := LexModeName(tt.id).validate() + if tt.invalid { + if err == nil { + t.Errorf("expected error didn't occur") + } + } else { + if err != nil { + t.Errorf("unexpected error occurred: %v", err) + } + } + }) + } +} + +func TestSnakeCaseToUpperCamelCase(t *testing.T) { + tests := []struct { + snake string + camel string + }{ + { + snake: "foo", + camel: "Foo", + }, + { + snake: "foo_bar", + camel: "FooBar", + }, + { + snake: "foo_bar_baz", + camel: "FooBarBaz", + }, + { + snake: "Foo", + camel: "Foo", + }, + { + snake: "fooBar", + camel: "FooBar", + }, + { + snake: "FOO", + camel: "FOO", + }, + { + snake: "FOO_BAR", + camel: "FOOBAR", + }, + { + snake: "_foo_bar_", + camel: "FooBar", + }, + { + snake: "___foo___bar___", + camel: "FooBar", + }, + } + for _, tt := range tests { + c := SnakeCaseToUpperCamelCase(tt.snake) + if c != tt.camel { + t.Errorf("unexpected string; want: %v, got: %v", tt.camel, c) + } + } +} + +func TestFindSpellingInconsistencies(t *testing.T) { + tests := []struct { + ids []string + duplicated [][]string + }{ + { + ids: []string{"foo", "foo"}, + duplicated: nil, + }, + { + ids: []string{"foo", "Foo"}, + duplicated: [][]string{{"Foo", "foo"}}, + }, + { + ids: []string{"foo", "foo", "Foo"}, + duplicated: [][]string{{"Foo", "foo"}}, + }, + { + ids: []string{"foo_bar_baz", "FooBarBaz"}, + duplicated: [][]string{{"FooBarBaz", "foo_bar_baz"}}, + }, + { + ids: []string{"foo", "Foo", "bar", "Bar"}, + duplicated: [][]string{{"Bar", "bar"}, {"Foo", "foo"}}, + }, + { + ids: []string{"foo", "Foo", "bar", "Bar", "baz", "bra"}, + duplicated: [][]string{{"Bar", "bar"}, {"Foo", "foo"}}, + }, + } + for i, tt := range tests { + t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) { + duplicated := FindSpellingInconsistencies(tt.ids) + if len(duplicated) != len(tt.duplicated) { + t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated, duplicated) + } + for i, dupIDs := range duplicated { + if len(dupIDs) != len(tt.duplicated[i]) { + t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated[i], dupIDs) + } + for j, id := range dupIDs { + if id != tt.duplicated[i][j] { + t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated[i], dupIDs) + } + } + } + }) + } +} + +func TestLexSpec_Validate(t *testing.T) { + // We expect that the spelling inconsistency error will occur. + spec := &LexSpec{ + Entries: []*LexEntry{ + { + Modes: []LexModeName{ + // 'Default' is the spelling inconsistency because 'default' is predefined. + "Default", + }, + Kind: "foo", + Pattern: "foo", + }, + }, + } + err := spec.Validate() + if err == nil { + t.Fatalf("expected error didn't occur") + } +} |