Skip to content

Commit

Permalink
test: generate utf-16 testdata and bom variants
Browse files Browse the repository at this point in the history
  • Loading branch information
neilpa committed Sep 18, 2020
1 parent 6077a84 commit 6f93deb
Show file tree
Hide file tree
Showing 50 changed files with 114 additions and 30 deletions.
57 changes: 57 additions & 0 deletions gen_testdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// +build ignore

// generates clones the utf-8 tests data to the other
// unicode encodings and adds BOM variants of each.
package main

import (
"io/ioutil"
"log"
"os"
"path/filepath"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
)


func main() {
var xforms = []struct {
dir, bom string
enc encoding.Encoding
} {
{ "testdata/utf-16be", "\xFE\xFF", unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM) },
{ "testdata/utf-16le", "\xFF\xFE", unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM) },
}

paths, _ := filepath.Glob("testdata/utf-8/*")
for _, p := range paths {
src, err := ioutil.ReadFile(p)
if err != nil {
log.Fatal(err)
}

write("testdata/utf-8_bom", p, "\xEF\xBB\xBF", src)
for _, xform := range xforms {
dst, err := xform.enc.NewEncoder().Bytes(src)
if err != nil {
log.Fatal(err)
}
write(xform.dir, p, "", dst)
write(xform.dir + "_bom", p, xform.bom, dst)
}
}
}

func write(dir, orig, bom string, buf []byte) {
f, err := os.Create(filepath.Join(dir, filepath.Base(orig)))
if err != nil {
log.Fatal(err)
}
if _, err = f.Write([]byte(bom)); err != nil {
log.Fatal(err)
}
if _, err = f.Write(buf); err != nil {
log.Fatal(err)
}
}
59 changes: 29 additions & 30 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,61 @@ import (

func TestMain(t *testing.T) {
tests := []struct {
in string
out []string
in string
out []string
exit int
} {
}{
{
"-s testdata/schema.yml testdata/data-pass.yml",
[]string{"testdata/data-pass.yml: pass"},
"-s testdata/utf-8/schema.yml testdata/utf-8/data-pass.yml",
[]string{"testdata/utf-8/data-pass.yml: pass"},
0,
}, {
"-s testdata/schema.json testdata/data-pass.yml",
[]string{"testdata/data-pass.yml: pass"},
"-s testdata/utf-8/schema.json testdata/utf-8/data-pass.yml",
[]string{"testdata/utf-8/data-pass.yml: pass"},
0,
}, {
"-s testdata/schema.json testdata/data-pass.json",
[]string{"testdata/data-pass.json: pass"},
"-s testdata/utf-8/schema.json testdata/utf-8/data-pass.json",
[]string{"testdata/utf-8/data-pass.json: pass"},
0,
}, {
"-s testdata/schema.yml testdata/data-pass.json",
[]string{"testdata/data-pass.json: pass"},
"-s testdata/utf-8/schema.yml testdata/utf-8/data-pass.json",
[]string{"testdata/utf-8/data-pass.json: pass"},
0,
}, {
"-q -s testdata/schema.yml testdata/data-fail.yml",
[]string{"testdata/data-fail.yml: fail: (root): foo is required"},
"-q -s testdata/utf-8/schema.yml testdata/utf-8/data-fail.yml",
[]string{"testdata/utf-8/data-fail.yml: fail: (root): foo is required"},
1,
}, {
"-q -s testdata/schema.json testdata/data-fail.yml",
[]string{"testdata/data-fail.yml: fail: (root): foo is required"},
"-q -s testdata/utf-8/schema.json testdata/utf-8/data-fail.yml",
[]string{"testdata/utf-8/data-fail.yml: fail: (root): foo is required"},
1,
}, {
"-q -s testdata/schema.json testdata/data-fail.json",
[]string{"testdata/data-fail.json: fail: (root): foo is required"},
"-q -s testdata/utf-8/schema.json testdata/utf-8/data-fail.json",
[]string{"testdata/utf-8/data-fail.json: fail: (root): foo is required"},
1,
}, {
"-q -s testdata/schema.yml testdata/data-fail.json",
[]string{"testdata/data-fail.json: fail: (root): foo is required"},
"-q -s testdata/utf-8/schema.yml testdata/utf-8/data-fail.json",
[]string{"testdata/utf-8/data-fail.json: fail: (root): foo is required"},
1,
}, {
"-q -s testdata/schema.json testdata/data-error.json",
[]string{"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"},
"-q -s testdata/utf-8/schema.json testdata/utf-8/data-error.json",
[]string{"testdata/utf-8/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"},
2,
}, {
"-q -s testdata/schema.yml testdata/data-error.yml",
[]string{"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream"},
"-q -s testdata/utf-8/schema.yml testdata/utf-8/data-error.yml",
[]string{"testdata/utf-8/data-error.yml: error: load doc: yaml: found unexpected end of stream"},
2,
}, {
"-q -s testdata/schema.json testdata/data-*.json",
"-q -s testdata/utf-8/schema.json testdata/utf-8/data-*.json",
[]string{
"testdata/data-fail.json: fail: (root): foo is required",
"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')",
"testdata/utf-8/data-fail.json: fail: (root): foo is required",
"testdata/utf-8/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')",
}, 3,
}, {
"-q -s testdata/schema.yml testdata/data-*.yml",
"-q -s testdata/utf-8/schema.yml testdata/utf-8/data-*.yml",
[]string{
"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream",
"testdata/data-fail.yml: fail: (root): foo is required",
"testdata/utf-8/data-error.yml: error: load doc: yaml: found unexpected end of stream",
"testdata/utf-8/data-fail.yml: fail: (root): foo is required",
}, 3,
},
}
Expand All @@ -89,4 +89,3 @@ func TestMain(t *testing.T) {
})
}
}

Binary file added testdata/utf-16be/data-error.json
Binary file not shown.
Binary file added testdata/utf-16be/data-error.yml
Binary file not shown.
Binary file added testdata/utf-16be/data-fail.json
Binary file not shown.
Binary file added testdata/utf-16be/data-fail.yml
Binary file not shown.
Binary file added testdata/utf-16be/data-pass.json
Binary file not shown.
Binary file added testdata/utf-16be/data-pass.yml
Binary file not shown.
Binary file added testdata/utf-16be/schema.json
Binary file not shown.
Binary file added testdata/utf-16be/schema.yml
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-error.json
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-error.yml
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-fail.json
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-fail.yml
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-pass.json
Binary file not shown.
Binary file added testdata/utf-16be_bom/data-pass.yml
Binary file not shown.
Binary file added testdata/utf-16be_bom/schema.json
Binary file not shown.
Binary file added testdata/utf-16be_bom/schema.yml
Binary file not shown.
Binary file added testdata/utf-16le/data-error.json
Binary file not shown.
Binary file added testdata/utf-16le/data-error.yml
Binary file not shown.
Binary file added testdata/utf-16le/data-fail.json
Binary file not shown.
Binary file added testdata/utf-16le/data-fail.yml
Binary file not shown.
Binary file added testdata/utf-16le/data-pass.json
Binary file not shown.
Binary file added testdata/utf-16le/data-pass.yml
Binary file not shown.
Binary file added testdata/utf-16le/schema.json
Binary file not shown.
Binary file added testdata/utf-16le/schema.yml
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-error.json
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-error.yml
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-fail.json
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-fail.yml
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-pass.json
Binary file not shown.
Binary file added testdata/utf-16le_bom/data-pass.yml
Binary file not shown.
Binary file added testdata/utf-16le_bom/schema.json
Binary file not shown.
Binary file added testdata/utf-16le_bom/schema.yml
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/utf-8_bom/data-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not valid json
1 change: 1 addition & 0 deletions testdata/utf-8_bom/data-error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid: "an escaped \' single quote is not valid yaml
3 changes: 3 additions & 0 deletions testdata/utf-8_bom/data-fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bar": "missing foo"
}
2 changes: 2 additions & 0 deletions testdata/utf-8_bom/data-fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
bar: missing foo
4 changes: 4 additions & 0 deletions testdata/utf-8_bom/data-pass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "asdf",
"bar": "zxcv"
}
3 changes: 3 additions & 0 deletions testdata/utf-8_bom/data-pass.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
foo: asdf
bar: zxcv
7 changes: 7 additions & 0 deletions testdata/utf-8_bom/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"properties": {
"foo": { "type": "string" },
"bar": {}
},
"required": ["foo"]
}
7 changes: 7 additions & 0 deletions testdata/utf-8_bom/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
properties:
foo:
type: string
bar: {}
required:
- foo

0 comments on commit 6f93deb

Please sign in to comment.