// Diff gets a string describing the differences between the arguments // and the specified objects. // // Returns the diff string and number of differences found. func (args Arguments) Diff(objects []interface{}) (string, int) { var output string = "\n" var differences int var maxArgCount int = len(args) if len(objects) > maxArgCount { maxArgCount = len(objects) } for i := 0; i < maxArgCount; i++ { var actual, expected interface{} if len(objects) <= i { actual = "(Missing)" } else { actual = objects[i] } if len(args) <= i { expected = "(Missing)" } else { expected = args[i] } if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() { // type checking if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) { // not match differences++ output = fmt.Sprintf("%s\t%d: \u274C type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actual) } } else { // normal checking if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { // match output = fmt.Sprintf("%s\t%d: \u2705 %s == %s\n", output, i, actual, expected) } else { // not match differences++ output = fmt.Sprintf("%s\t%d: \u274C %s != %s\n", output, i, actual, expected) } } } if differences == 0 { return "No differences.", differences } return output, differences }
func TestSortableIdentifierSlice(t *testing.T) { e1 := MyEntity1{} e1.BasicEntity = NewBasic() e2 := MyEntity1{} e2.BasicEntity = NewBasic() var entities IdentifierSlice = []Identifier{e2, e1} sort.Sort(entities) assert.ObjectsAreEqual(e1, entities[0]) assert.ObjectsAreEqual(e2, entities[1]) }
func (suite *datastoreTestSuite) TestGetModelsWithKeys() { srcModels := loadTestData(suite.GAEContext) destModels := make([]models.Model, 5) destTest := make([]*testModel, 5) var ( key *datastore.Key err error ) for index, _ := range destTest { key, err = srcModels[index].Key() destTest[index] = new(testModel) destTest[index].SetKey(key) destModels[index] = destTest[index] } err = GetModelsWithKeys(suite.GAEContext, destModels) assert.Nil(suite.T, err, "GetModelsWithKeys shouldn't error") for index, model := range destTest { assert.ObjectsAreEqual(model, srcModels[index]) key, err = model.Key() assert.Nil(suite.T, err, "Models should have keys after running Get") assert.NotNil(suite.T, key, "Models should have keys after running Get") } }
// parseTcpRequestResponse parses a request then a response packet and validates // the published result. func parseTcpRequestResponse(t testing.TB, dns *Dns, q DnsTestMessage) { var private protos.ProtocolData packet := newPacket(forward, q.request) tcptuple := testTcpTuple() private = dns.Parse(packet, tcptuple, tcp.TcpDirectionOriginal, private) packet = newPacket(reverse, q.response) dns.Parse(packet, tcptuple, tcp.TcpDirectionReverse, private) assert.Empty(t, dns.transactions.Size(), "There should be no transactions.") m := expectResult(t, dns) assert.Equal(t, "tcp", mapValue(t, m, "transport")) assert.Equal(t, len(q.request), mapValue(t, m, "bytes_in")) assert.Equal(t, len(q.response), mapValue(t, m, "bytes_out")) assert.NotNil(t, mapValue(t, m, "responsetime")) if assert.ObjectsAreEqual("NOERROR", mapValue(t, m, "dns.response_code")) { assert.Equal(t, common.OK_STATUS, mapValue(t, m, "status")) } else { assert.Equal(t, common.ERROR_STATUS, mapValue(t, m, "status")) } assert.Nil(t, mapValue(t, m, "notes")) assertMapStrData(t, m, q) }
func assertHasLine(t *testing.T, tr Transaction, expected Line) { for _, l := range tr.Lines { if assert.ObjectsAreEqual(expected, l) { return } } assert.Fail(t, "line not found", expected.String()) }
func TestGetDocument(t *testing.T) { es := newEsTestClient(t).recreateIndex(estestIndex) id := "1" doc := map[string]interface{}{"name": id} es.putDocument(estestIndex, estestType, id, "", doc) returned := es.getDocumentMap(estestIndex, estestType, id, "") assert.ObjectsAreEqual(doc, returned) }
func (suite *ConfigSuite) TestMarshalRoundtrip() { configBytes, err := yaml.Marshal(suite.expectedConfig) assert.Nil(suite.T(), err) var config Configuration err = yaml.Unmarshal(configBytes, &config) assert.Nil(suite.T(), err) assert.True(suite.T(), assert.ObjectsAreEqual(config, suite.expectedConfig)) }
// Diff returns the values in v that differ from the values in other. // ISO 31-11: v \ other func (v Values) Diff(other Values) Values { diff := Values{} for key, value := range v { if !assert.ObjectsAreEqual(value, other[key]) { diff[key] = value } } return diff }
func TestCommandClone(t *testing.T) { nilCmds := Commands(nil) assert.Equal(t, nilCmds.Clone(), Commands(nil)) manyCmds := Commands{ Command("1"), Command("2"), } assert.True(t, assert.ObjectsAreEqual(manyCmds.Clone(), manyCmds), ) }
func (suite *datastoreTestSuite) TestGetModel() { models := loadTestData(suite.GAEContext) src := models[0] key, err := src.Key() assert.NotNil(suite.T, key, "The src model should have its key set") dest := new(testModel) err = GetModel(suite.GAEContext, key, dest) assert.Nil(suite.T, err, "GetModel shouldn't return an error") assert.ObjectsAreEqual(src, dest) }
func TestMultipleEncodingDecodeEvent(t *testing.T) { cases := []struct { ev *sse }{ {ev}, {newSSEEvent("102", "DUP")}, {newSSEEvent("103", "FUN")}, } r, w := io.Pipe() enc := newEncoder(w) assert.NotNil(t, enc) dec := newDecoder(r) assert.NotNil(t, dec) var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() defer w.Close() for _, tc := range cases { err := enc.Encode(tc.ev) assert.Nil(t, err) } }() go func() { defer wg.Done() defer r.Close() for { inputEv, err := dec.Decode() if err == io.EOF { break } exists := false for _, tc := range cases { exists = exists || assert.ObjectsAreEqual(tc.ev, inputEv) } assert.NoError(t, err) assert.True(t, exists) } }() wg.Wait() }
// SQL tests that the given Compiles instance matches the expected string for // the current dialect. func (t *tester) SQL(expect string, stmt Compiles, ps ...interface{}) { // Get caller information in case of failure caller := callerInfo() // Start a new parameters instance params := Params() // Compile the given stmt with the tester's dialect actual, err := stmt.Compile(t.dialect, params) if err != nil { t.t.Errorf("%s: unexpected error from Compile(): %s", caller, err) return } if expect != actual { t.t.Errorf( "%s: unexpected SQL from Compile(): expect %s, got %s", caller, expect, actual, ) } // Test that the parameters are equal if len(*params) != len(ps) { t.t.Errorf( "%s: unexpected number of parameters for %s: expect %d, got %d", caller, actual, len(ps), len(*params), ) return } // Examine individual parameters for equality for i, param := range *params { if !assert.ObjectsAreEqual(ps[i], param) { t.t.Errorf( "%s: unequal parameters at index %d: expect %#v, got %#v", caller, i, ps[i], param, ) } } }
// parseUdpRequestResponse parses a request then a response packet and validates // the published result. func parseUDPRequestResponse(t testing.TB, dns *dnsPlugin, q dnsTestMessage) { packet := newPacket(forward, q.request) dns.ParseUDP(packet) packet = newPacket(reverse, q.response) dns.ParseUDP(packet) assert.Empty(t, dns.transactions.Size(), "There should be no transactions.") m := expectResult(t, dns) assert.Equal(t, "udp", mapValue(t, m, "transport")) assert.Equal(t, len(q.request), mapValue(t, m, "bytes_in")) assert.Equal(t, len(q.response), mapValue(t, m, "bytes_out")) assert.NotNil(t, mapValue(t, m, "responsetime")) if assert.ObjectsAreEqual("NOERROR", mapValue(t, m, "dns.response_code")) { assert.Equal(t, common.OK_STATUS, mapValue(t, m, "status")) } else { assert.Equal(t, common.ERROR_STATUS, mapValue(t, m, "status")) } assert.Nil(t, mapValue(t, m, "notes")) assertMapStrData(t, m, q) }
// Verify that the split lone request packet is parsed. func TestParseTcpSplitResponse(t *testing.T) { dns := newDns(testing.Verbose()) tcpQuery := elasticATcp q := tcpQuery.request r0 := tcpQuery.response[:10] r1 := tcpQuery.response[10:] tcptuple := testTcpTuple() private := protos.ProtocolData(new(dnsPrivateData)) packet := newPacket(forward, q) private = dns.Parse(packet, tcptuple, tcp.TcpDirectionOriginal, private) assert.Equal(t, 1, dns.transactions.Size(), "There should be one transaction.") packet = newPacket(reverse, r0) private = dns.Parse(packet, tcptuple, tcp.TcpDirectionReverse, private) assert.Equal(t, 1, dns.transactions.Size(), "There should be one transaction.") packet = newPacket(reverse, r1) private = dns.Parse(packet, tcptuple, tcp.TcpDirectionReverse, private) assert.Empty(t, dns.transactions.Size(), "There should be no transaction.") m := expectResult(t, dns) assert.Equal(t, "tcp", mapValue(t, m, "transport")) assert.Equal(t, len(tcpQuery.request), mapValue(t, m, "bytes_in")) assert.Equal(t, len(tcpQuery.response), mapValue(t, m, "bytes_out")) assert.NotNil(t, mapValue(t, m, "responsetime")) if assert.ObjectsAreEqual("NOERROR", mapValue(t, m, "dns.response_code")) { assert.Equal(t, common.OK_STATUS, mapValue(t, m, "status")) } else { assert.Equal(t, common.ERROR_STATUS, mapValue(t, m, "status")) } assert.Nil(t, mapValue(t, m, "notes")) assertMapStrData(t, m, tcpQuery) }
func TestNewClient_httpClient(t *testing.T) { transport := &http.Transport{Proxy: http.ProxyFromEnvironment} httpClient := &http.Client{Transport: transport} client := NewClient(httpClient, testClientToken) assert.ObjectsAreEqual(httpClient, client.Client) }
func (suite *ConfigSuite) TestParseSimple() { var config Configuration err := yaml.Unmarshal([]byte(configYaml), &config) assert.Nil(suite.T(), err) assert.True(suite.T(), assert.ObjectsAreEqual(config, suite.expectedConfig)) }
func Test_FindAbility(t *testing.T) { ability, ok := FindAbility("Fireball 3") assert.True(t, ok, "Fireball ability should be found") assert.ObjectsAreEqual(&Fireball{Level: 3}, ability) }