Example #1
0
func TestNtfns(t *testing.T) {
	for _, test := range ntfntests {
		// create notification.
		n := test.f()

		// verify that id is nil.
		if n.Id() != nil {
			t.Errorf("%s: notification should not have non-nil id %v",
				test.name, n.Id())
			continue
		}

		mn, err := n.MarshalJSON()
		if err != nil {
			t.Errorf("%s: failed to marshal notification: %v",
				test.name, err)
			continue
		}

		n2, err := btcjson.ParseMarshaledCmd(mn)
		if err != nil {
			t.Errorf("%s: failed to ummarshal cmd: %v",
				test.name, err)
			continue
		}

		if !reflect.DeepEqual(test.result, n2) {
			t.Errorf("%s: unmarshal not as expected. "+
				"got %v wanted %v", test.name, spew.Sdump(n2),
				spew.Sdump(test.result))
		}
		if !reflect.DeepEqual(n, n2) {
			t.Errorf("%s: unmarshal not as we started with. "+
				"got %v wanted %v", test.name, spew.Sdump(n2),
				spew.Sdump(n))
		}

		// Read marshaled notification back into n.  Should still
		// match result.
		if err := n.UnmarshalJSON(mn); err != nil {
			t.Errorf("%s: unmarshal failed: %v", test.name, err)
			continue
		}
		if !reflect.DeepEqual(test.result, n) {
			t.Errorf("%s: unmarshal not as expected. "+
				"got %v wanted %v", test.name, spew.Sdump(n),
				spew.Sdump(test.result))
		}
	}
}
Example #2
0
func TestCmds(t *testing.T) {
	for _, test := range cmdtests {
		c, err := test.f()
		if err != nil {
			t.Errorf("%s: failed to run func: %v",
				test.name, err)
			continue
		}

		mc, err := c.MarshalJSON()
		if err != nil {
			t.Errorf("%s: failed to marshal cmd: %v",
				test.name, err)
			continue
		}

		c2, err := btcjson.ParseMarshaledCmd(mc)
		if err != nil {
			t.Errorf("%s: failed to ummarshal cmd: %v",
				test.name, err)
			continue
		}

		if !reflect.DeepEqual(test.result, c2) {
			t.Errorf("%s: unmarshal not as expected. "+
				"got %v wanted %v", test.name, spew.Sdump(c2),
				spew.Sdump(test.result))
		}
		if !reflect.DeepEqual(c, c2) {
			t.Errorf("%s: unmarshal not as we started with. "+
				"got %v wanted %v", test.name, spew.Sdump(c2),
				spew.Sdump(c))
		}

		// id from Id func must match result.
		if c.Id() != test.result.Id() {
			t.Errorf("%s: Id returned incorrect id. "+
				"got %v wanted %v", test.name, c.Id(),
				test.result.Id())
		}

		// method from Method func must match result.
		if c.Method() != test.result.Method() {
			t.Errorf("%s: Method returned incorrect method. "+
				"got %v wanted %v", test.name, c.Method(),
				test.result.Method())
		}

		// Read marshaled command back into c.  Should still
		// match result.
		if err := c.UnmarshalJSON(mc); err != nil {
			t.Errorf("%s: error while unmarshalling: %v", test.name,
				err)
		}
		if !reflect.DeepEqual(test.result, c) {
			t.Errorf("%s: unmarshal not as expected. "+
				"got %v wanted %v", test.name, spew.Sdump(c),
				spew.Sdump(test.result))
		}
	}
}