|
| 1 | +package codemeta_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/git-pkgs/codemeta" |
| 8 | +) |
| 9 | + |
| 10 | +func TestDocumentStrings(t *testing.T) { |
| 11 | + for _, tc := range []struct { |
| 12 | + name, input, term string |
| 13 | + want []string |
| 14 | + }{ |
| 15 | + {"scalar", `{"keywords":"science"}`, "keywords", []string{"science"}}, |
| 16 | + {"array", `{"keywords":["science","metadata"]}`, "keywords", []string{"science", "metadata"}}, |
| 17 | + {"set", `{"keywords":{"@set":["science","metadata"]}}`, "keywords", []string{"science", "metadata"}}, |
| 18 | + {"list", `{"keywords":{"@list":["science",{"@value":"metadata"}]}}`, "keywords", []string{"science", "metadata"}}, |
| 19 | + {"id alias", `{"@context":"https://w3id.org/codemeta/3.0","license":{"id":"https://spdx.org/licenses/MIT"}}`, "license", []string{"https://spdx.org/licenses/MIT"}}, |
| 20 | + {"prefixed names", `{"@context":"https://w3id.org/codemeta/3.0","schema:programmingLanguage":[{"schema:name":"Go"},{"http://schema.org/name":"Ruby"}]}`, "programmingLanguage", []string{"Go", "Ruby"}}, |
| 21 | + {"identifier first", `{"license":{"@id":"MIT","name":"MIT License"}}`, "license", []string{"MIT"}}, |
| 22 | + {"empty identifier", `{"license":{"@id":"","name":"MIT License"}}`, "license", []string{"MIT License"}}, |
| 23 | + {"exact key first", `{"@context":"https://w3id.org/codemeta/3.0","programmingLanguage":{"name":"Go","schema:name":"Ruby"}}`, "programmingLanguage", []string{"Go"}}, |
| 24 | + {"local context", `{"programmingLanguage":{"@context":"https://w3id.org/codemeta/3.0","schema:name":"Go"}}`, "programmingLanguage", []string{"Go"}}, |
| 25 | + {"reset context", `{"@context":"https://w3id.org/codemeta/3.0","programmingLanguage":{"@context":null,"schema:name":"Go"}}`, "programmingLanguage", nil}, |
| 26 | + {"unsupported context", `{"@context":"https://example.org/context","programmingLanguage":{"schema:name":"Go"}}`, "programmingLanguage", nil}, |
| 27 | + {"scalars", `{"keywords":[null,4,true,"",{},"science"]}`, "keywords", []string{"4", "true", "science"}}, |
| 28 | + {"number spelling", `{"copyrightYear":2020}`, "copyrightYear", []string{"2020"}}, |
| 29 | + {"number value object", `{"copyrightYear":{"@value":2020}}`, "copyrightYear", []string{"2020"}}, |
| 30 | + {"missing", `{}`, "keywords", nil}, |
| 31 | + } { |
| 32 | + t.Run(tc.name, func(t *testing.T) { |
| 33 | + doc, err := codemeta.Parse([]byte(tc.input)) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + before := doc.Get(tc.term) |
| 38 | + if got := doc.Strings(tc.term); !reflect.DeepEqual(got, tc.want) { |
| 39 | + t.Fatalf("Strings(%q) = %v, want %v", tc.term, got, tc.want) |
| 40 | + } |
| 41 | + if !reflect.DeepEqual(doc.Get(tc.term), before) { |
| 42 | + t.Fatal("projection changed the source value") |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
0 commit comments