<commit_before>// Copyright 2017 The ObjectHash-Proto Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tests

import (
	"fmt"

	"github.com/benlaurie/objecthash/go/objecthash"
	"github.com/golang/protobuf/proto"
)

type testCase struct {
	protos               []proto.Message
	equivalentObject     interface{}
	equivalentJsonString string
	expectedHashString   string
}

func (tc testCase) check(hasher ProtoHasher) error {
	for _, message := range tc.protos {
		messageHash, err := hasher.HashProto(message)
		if err != nil {
			return fmt.Errorf("Attempting to hash %T{ %+v } returned an error.", message, message)
		}
		messageHashStr := fmt.Sprintf("%x", messageHash)

		// If the test case has an expected hash string, check it.
		if tc.expectedHashString != "" {
			if messageHashStr != tc.expectedHashString {
				return fmt.Errorf("Got the wrong objecthash for %T{ %+v }.\n"+
					"Actual:   %v\nExpected: %v\n", message, message, messageHashStr, tc.expectedHashString)
			}
		}

		// If the test case has an equivalent JSON String, check it.
		if tc.equivalentJsonString != "" {
			commonJsonHash, err := objecthash.CommonJSONHash(tc.equivalentJsonString)
			if err != nil {
				return fmt.Errorf("Attempting to hash %+v returned an error.", tc.equivalentJsonString)
			}
			commonJsonHashStr := fmt.Sprintf("%x", commonJsonHash)

			if messageHashStr != commonJsonHashStr {
				return fmt.Errorf("The objecthash for %T{ %+v } was expected to be the same as that of %+v.\n"+
					"Actual:   %v\nExpected: %v\n", message, message, tc.equivalentJsonString, messageHashStr, commonJsonHashStr)
			}
		}

		// If the test case has an equivalent object, check it.
		if tc.equivalentObject != nil {
			equivalentObjectHash, err := objecthash.ObjectHash(tc.equivalentObject)
			if err != nil {
				return fmt.Errorf("Attempting to hash %+v returned an error.", tc.equivalentObject)
			}
			equivalentObjectHashStr := fmt.Sprintf("%x", equivalentObjectHash)

			if messageHashStr != equivalentObjectHashStr {
				return fmt.Errorf("The objecthash for %T{ %+v } was expected to be the same as that of %+v.\n"+
					"Actual:   %v\nExpected: %v\n", message, message, tc.equivalentObject, messageHashStr, equivalentObjectHashStr)
			}
		}
	}

	return nil
}
<commit_msg>Improve the error reporting for test case checking.<commit_after>// Copyright 2017 The ObjectHash-Proto Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tests

import (
	"fmt"

	"github.com/benlaurie/objecthash/go/objecthash"
	"github.com/golang/protobuf/proto"
)

type testCase struct {
	protos               []proto.Message
	equivalentObject     interface{}
	equivalentJsonString string
	expectedHashString   string
}

func (tc testCase) check(hasher ProtoHasher) error {
	for _, message := range tc.protos {
		messageHash, err := hasher.HashProto(message)
		if err != nil {
			return fmt.Errorf("Attempting to hash %T{ %[1]v } returned an error: %v", message, err)
		}
		messageHashStr := fmt.Sprintf("%x", messageHash)

		// If the test case has an expected hash string, check it.
		if tc.expectedHashString != "" {
			if messageHashStr != tc.expectedHashString {
				return fmt.Errorf("Got the wrong objecthash for %T{ %[1]v }.\n"+
					"Actual:   %v\nExpected: %v\n", message, messageHashStr, tc.expectedHashString)
			}
		}

		// If the test case has an equivalent JSON String, check it.
		if tc.equivalentJsonString != "" {
			commonJsonHash, err := objecthash.CommonJSONHash(tc.equivalentJsonString)
			if err != nil {
				return fmt.Errorf("Attempting to hash %+v returned an error: %v", tc.equivalentJsonString, err)
			}
			commonJsonHashStr := fmt.Sprintf("%x", commonJsonHash)

			if messageHashStr != commonJsonHashStr {
				return fmt.Errorf("The objecthash for %T{ %[1]v } was expected to be the same as that of %+v.\n"+
					"Actual:   %v\nExpected: %v\n", message, tc.equivalentJsonString, messageHashStr, commonJsonHashStr)
			}
		}

		// If the test case has an equivalent object, check it.
		if tc.equivalentObject != nil {
			equivalentObjectHash, err := objecthash.ObjectHash(tc.equivalentObject)
			if err != nil {
				return fmt.Errorf("Attempting to hash %+v returned an error: %v", tc.equivalentObject, err)
			}
			equivalentObjectHashStr := fmt.Sprintf("%x", equivalentObjectHash)

			if messageHashStr != equivalentObjectHashStr {
				return fmt.Errorf("The objecthash for %T{ %[1]v } was expected to be the same as that of %+v.\n"+
					"Actual:   %v\nExpected: %v\n", message, tc.equivalentObject, messageHashStr, equivalentObjectHashStr)
			}
		}
	}

	return nil
}

{'fraction_non_alphanumeric': 0.11160555357778569, 'fraction_numerical': 0.00355998576005696, 'mean_word_length': 3.6592039800995027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104447', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1663, 'n_words': 571}