Ejemplo n.º 1
1
// compareFileGraphs compares |expected| and |actual| and returns a non-nil
// error if they are not deeply equal. The error message contains a human-readable
// string containing a deep-print of expected and actual along with the substrings
// starting from the first character where they differ.
func compareFileGraphs(expected *mojom_files.MojomFileGraph, actual *mojom_files.MojomFileGraph) error {
	if !reflect.DeepEqual(expected, actual) {
		// Note(rudominer) The myfmt package is a local modification of the fmt package
		// that does a deep printing that follows pointers for up to 50 levels.
		// Thus expectedString and actualString should contain enough information to
		// precisely capture the structure of expected and actual.
		expectedString := myfmt.Sprintf("%#v", expected)
		actualString := myfmt.Sprintf("%#v", actual)
		if expectedString != actualString {
			diffPos := -1
			for i := 0; i < len(expectedString) && i < len(actualString); i++ {
				if expectedString[i] != actualString[i] {
					diffPos = i
					break
				}
			}
			mismatchExpected := ""
			mismatchActual := ""
			if diffPos > -1 {
				mismatchExpected = expectedString[diffPos:]
				mismatchActual = actualString[diffPos:]
			}
			return fmt.Errorf("*****\nexpected=\n*****\n%q\n*****\nactual=\n*****\n%q\n*****\n"+
				"match failed at position %d: expected=\n*****\n%q\n******\nactual=\n*****\n%q\n******\n",
				expectedString, actualString, diffPos, mismatchExpected, mismatchActual)
		} else {
			return fmt.Errorf("expected != actual but the two printed equal.")
		}
	}
	return nil
}
Ejemplo n.º 2
1
// Serialize serializes the MojomDescriptor into a binary form that is passed to the
// backend of the compiler in order to invoke the code generators.
// To do this we use Mojo serialization.
// If |debug| is true we also return a human-readable representation
// of the serialized mojom_types.FileGraph.
func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString string, err error) {
	fileGraph := translateDescriptor(d)
	if debug {
		debugString = myfmt.Sprintf("%#v", fileGraph)
	}
	encoder := bindings.NewEncoder()
	fileGraph.Encode(encoder)
	bytes, _, err = encoder.Data()
	return
}