forked from a-h/generate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
8,859 additions
and
1,582 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/schema-generate | ||
.idea | ||
test/*_gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: go | ||
|
||
go_import_path: github.com/a-h/generate | ||
|
||
script: | ||
- make codecheck | ||
- make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/a-h/generate" | ||
) | ||
|
||
func TestThatFieldNamesAreOrdered(t *testing.T) { | ||
m := map[string]generate.Field{ | ||
"z": {}, | ||
"b": {}, | ||
} | ||
|
||
actual := getOrderedFieldNames(m) | ||
expected := []string{"b", "z"} | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("expected %s and actual %s should match in order", strings.Join(expected, ", "), strings.Join(actual, ",")) | ||
} | ||
} | ||
|
||
func TestThatStructNamesAreOrdered(t *testing.T) { | ||
m := map[string]generate.Struct{ | ||
"c": {}, | ||
"b": {}, | ||
"a": {}, | ||
} | ||
|
||
actual := getOrderedStructNames(m) | ||
expected := []string{"a", "b", "c"} | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("expected %s and actual %s should match in order", strings.Join(expected, ", "), strings.Join(actual, ",")) | ||
} | ||
} | ||
|
||
func TestThatThePackageCanBeSet(t *testing.T) { | ||
pkg := "testpackage" | ||
p = &pkg | ||
|
||
r, w := io.Pipe() | ||
|
||
go output(w, make(map[string]generate.Struct), make(map[string]generate.Field)) | ||
|
||
lr := io.LimitedReader{R: r, N: 72} | ||
bs, _ := ioutil.ReadAll(&lr) | ||
output := bytes.NewBuffer(bs).String() | ||
|
||
want := `// Code generated by schema-generate. DO NOT EDIT. | ||
package testpackage | ||
` | ||
if output != want { | ||
t.Error("Unexpected package declaration: ", output) | ||
} | ||
} | ||
|
||
func TestLineAndCharacterFromOffset(t *testing.T) { | ||
tests := []struct { | ||
In []byte | ||
Offset int | ||
ExpectedLine int | ||
ExpectedCharacter int | ||
ExpectedError bool | ||
}{ | ||
{ | ||
In: []byte("Line 1\nLine 2"), | ||
Offset: 6, | ||
ExpectedLine: 2, | ||
ExpectedCharacter: 1, | ||
}, | ||
{ | ||
In: []byte("Line 1\r\nLine 2"), | ||
Offset: 7, | ||
ExpectedLine: 2, | ||
ExpectedCharacter: 1, | ||
}, | ||
{ | ||
In: []byte("Line 1\nLine 2"), | ||
Offset: 0, | ||
ExpectedLine: 1, | ||
ExpectedCharacter: 1, | ||
}, | ||
{ | ||
In: []byte("Line 1\nLine 2"), | ||
Offset: 200, | ||
ExpectedLine: 0, | ||
ExpectedCharacter: 0, | ||
ExpectedError: true, | ||
}, | ||
{ | ||
In: []byte("Line 1\nLine 2"), | ||
Offset: -1, | ||
ExpectedLine: 0, | ||
ExpectedCharacter: 0, | ||
ExpectedError: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
actualLine, actualCharacter, err := lineAndCharacter(test.In, test.Offset) | ||
if err != nil && !test.ExpectedError { | ||
t.Errorf("Unexpected error for input %s at offset %d: %v", test.In, test.Offset, err) | ||
continue | ||
} | ||
|
||
if actualLine != test.ExpectedLine || actualCharacter != test.ExpectedCharacter { | ||
t.Errorf("For '%s' at offset %d, expected %d:%d, but got %d:%d", test.In, test.Offset, test.ExpectedLine, test.ExpectedCharacter, actualLine, actualCharacter) | ||
} | ||
} | ||
} |
Oops, something went wrong.