| 1 | package jsonld |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestRef_MarshalText(t *testing.T) { |
| 11 | test := "test" |
| 12 | a := IRI(test) |
| 13 | |
| 14 | out, err := a.MarshalText() |
| 15 | if err != nil { |
| 16 | t.Errorf("Error %s", err) |
| 17 | } |
| 18 | if bytes.Compare(out, []byte(test)) != 0 { |
| 19 | t.Errorf("Invalid result '%s', expected '%s'", out, test) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestContext_MarshalJSON(t *testing.T) { |
| 24 | { |
| 25 | url := "test" |
| 26 | c := Context{{NilTerm, IRI(url)}} |
| 27 | |
| 28 | out, err := c.MarshalJSON() |
| 29 | if err != nil { |
| 30 | t.Errorf("%s", err) |
| 31 | } |
| 32 | if !strings.Contains(string(out), url) { |
| 33 | t.Errorf("Json doesn't contain %s, %s", url, string(out)) |
| 34 | } |
| 35 | jUrl, _ := json.Marshal(url) |
| 36 | if !bytes.Equal(jUrl, out) { |
| 37 | t.Errorf("Strings should be equal %s, %s", jUrl, out) |
| 38 | } |
| 39 | } |
| 40 | { |
| 41 | url := "example.com" |
| 42 | asTerm := "testingTerm##" |
| 43 | asUrl := "https://activitipubrocks.com" |
| 44 | c2 := Context{{NilTerm, IRI(url)}, {Term(asTerm), IRI(asUrl)}} |
| 45 | out, err := c2.MarshalJSON() |
| 46 | if err != nil { |
| 47 | t.Errorf("%s", err) |
| 48 | } |
| 49 | if !strings.Contains(string(out), url) { |
| 50 | t.Errorf("Json doesn't contain URL %s, %s", url, string(out)) |
| 51 | } |
| 52 | if !strings.Contains(string(out), asUrl) { |
| 53 | t.Errorf("Json doesn't contain URL %s, %s", asUrl, string(out)) |
| 54 | } |
| 55 | if !strings.Contains(string(out), asTerm) { |
| 56 | t.Errorf("Json doesn't contain Term %s, %s", asTerm, string(out)) |
| 57 | } |
| 58 | } |
| 59 | { |
| 60 | url := "test" |
| 61 | testTerm := "test_term" |
| 62 | asTerm := "testingTerm##" |
| 63 | asUrl := "https://activitipubrocks.com" |
| 64 | c3 := Context{{Term(testTerm), IRI(url)}, {Term(asTerm), IRI(asUrl)}} |
| 65 | out, err := c3.MarshalJSON() |
| 66 | if err != nil { |
| 67 | t.Errorf("%s", err) |
| 68 | } |
| 69 | if !strings.Contains(string(out), url) { |
| 70 | t.Errorf("Json doesn't contain URL %s, %s", url, string(out)) |
| 71 | } |
| 72 | if !strings.Contains(string(out), asUrl) { |
| 73 | t.Errorf("Json doesn't contain URL %s, %s", asUrl, string(out)) |
| 74 | } |
| 75 | if !strings.Contains(string(out), asTerm) { |
| 76 | t.Errorf("Json doesn't contain Term %s, %s", asTerm, string(out)) |
| 77 | } |
| 78 | if !strings.Contains(string(out), testTerm) { |
| 79 | t.Errorf("Json doesn't contain Term %s, %s", testTerm, string(out)) |
| 80 | } |
| 81 | } |
| 82 | { |
| 83 | url1 := "test" |
| 84 | url2 := "http://example.com" |
| 85 | c := Context{ |
| 86 | {IRI: IRI(url1)}, |
| 87 | {IRI: IRI(url2)}, |
| 88 | } |
| 89 | |
| 90 | out, err := c.MarshalJSON() |
| 91 | if err != nil { |
| 92 | t.Errorf("%s", err) |
| 93 | } |
| 94 | if !strings.Contains(string(out), url1) { |
| 95 | t.Errorf("Json doesn't contain %s, %s", url1, string(out)) |
| 96 | } |
| 97 | if !strings.Contains(string(out), url2) { |
| 98 | t.Errorf("Json doesn't contain %s, %s", url1, string(out)) |
| 99 | } |
| 100 | } |
| 101 | } |