MCPcopy Create free account
hub / github.com/segmentio/encoding / TestSetTrustRawMessage

Function TestSetTrustRawMessage

json/json_test.go:1759–1802  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1757}
1758
1759func TestSetTrustRawMessage(t *testing.T) {
1760 buf := &bytes.Buffer{}
1761 enc := NewEncoder(buf)
1762 enc.SetTrustRawMessage(true)
1763
1764 // "Good" values are encoded in the regular way
1765 m := map[string]json.RawMessage{
1766 "k": json.RawMessage(`"value"`),
1767 }
1768 if err := enc.Encode(m); err != nil {
1769 t.Error(err)
1770 }
1771
1772 b := buf.Bytes()
1773 exp := []byte(`{"k":"value"}`)
1774 exp = append(exp, '\n')
1775 if !bytes.Equal(exp, b) {
1776 t.Error(
1777 "unexpected encoding:",
1778 "expected", exp,
1779 "got", b,
1780 )
1781 }
1782
1783 // "Bad" values are encoded without checking and throwing an error
1784 buf.Reset()
1785 m = map[string]json.RawMessage{
1786 "k": json.RawMessage(`bad"value`),
1787 }
1788 if err := enc.Encode(m); err != nil {
1789 t.Error(err)
1790 }
1791
1792 b = buf.Bytes()
1793 exp = []byte(`{"k":bad"value}`)
1794 exp = append(exp, '\n')
1795 if !bytes.Equal(exp, b) {
1796 t.Error(
1797 "unexpected encoding:",
1798 "expected", exp,
1799 "got", b,
1800 )
1801 }
1802}
1803
1804func TestSetAppendNewline(t *testing.T) {
1805 buf := &bytes.Buffer{}

Callers

nothing calls this directly

Calls 6

SetTrustRawMessageMethod · 0.95
EncodeMethod · 0.95
BytesMethod · 0.80
NewEncoderFunction · 0.70
ErrorMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…