| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package serialization | 5 package serialization |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" |
| 9 "compress/gzip" |
| 10 "encoding/base64" |
| 8 "fmt" | 11 "fmt" |
| 12 "io/ioutil" |
| 9 "mojo/public/go/bindings" | 13 "mojo/public/go/bindings" |
| 10 "mojom/mojom_parser/generated/mojom_files" | 14 "mojom/mojom_parser/generated/mojom_files" |
| 11 "mojom/mojom_parser/generated/mojom_types" | 15 "mojom/mojom_parser/generated/mojom_types" |
| 12 "mojom/mojom_parser/mojom" | 16 "mojom/mojom_parser/mojom" |
| 13 "mojom/mojom_parser/parser" | 17 "mojom/mojom_parser/parser" |
| 14 "reflect" | 18 "reflect" |
| 15 "testing" | 19 "testing" |
| 16 myfmt "third_party/golang/src/fmt" | 20 myfmt "third_party/golang/src/fmt" |
| 17 ) | 21 ) |
| 18 | 22 |
| (...skipping 1996 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2015 t.Errorf("Serialization error for case %d: %s", i, err.E
rror()) | 2019 t.Errorf("Serialization error for case %d: %s", i, err.E
rror()) |
| 2016 continue | 2020 continue |
| 2017 } | 2021 } |
| 2018 | 2022 |
| 2019 // Deserialize | 2023 // Deserialize |
| 2020 decoder := bindings.NewDecoder(bytes, nil) | 2024 decoder := bindings.NewDecoder(bytes, nil) |
| 2021 fileGraph := mojom_files.MojomFileGraph{} | 2025 fileGraph := mojom_files.MojomFileGraph{} |
| 2022 fileGraph.Decode(decoder) | 2026 fileGraph.Decode(decoder) |
| 2023 | 2027 |
| 2024 // Deserialize RuntimeTypeInfo A | 2028 // Deserialize RuntimeTypeInfo A |
| 2025 » » decoder = bindings.NewDecoder(*fileGraph.Files[fileNameA].Serial
izedRuntimeTypeInfo, nil) | 2029 » » runtimeTypeInfoA := deserializeRuntimeTypeInfo(*fileGraph.Files[
fileNameA].SerializedRuntimeTypeInfo) |
| 2026 » » runtimeTypeInfoA := mojom_types.RuntimeTypeInfo{} | |
| 2027 » » runtimeTypeInfoA.Decode(decoder) | |
| 2028 | 2030 |
| 2029 // Deserialize RuntimeTypeInfo B | 2031 // Deserialize RuntimeTypeInfo B |
| 2030 » » decoder = bindings.NewDecoder(*fileGraph.Files[fileNameB].Serial
izedRuntimeTypeInfo, nil) | 2032 » » runtimeTypeInfoB := deserializeRuntimeTypeInfo(*fileGraph.Files[
fileNameB].SerializedRuntimeTypeInfo) |
| 2031 » » runtimeTypeInfoB := mojom_types.RuntimeTypeInfo{} | |
| 2032 » » runtimeTypeInfoB.Decode(decoder) | |
| 2033 | 2033 |
| 2034 // Compare A | 2034 // Compare A |
| 2035 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoA, &runti
meTypeInfoA); err != nil { | 2035 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoA, &runti
meTypeInfoA); err != nil { |
| 2036 t.Errorf("case %d A:\n%s", i, err.Error()) | 2036 t.Errorf("case %d A:\n%s", i, err.Error()) |
| 2037 } | 2037 } |
| 2038 | 2038 |
| 2039 // Compare B | 2039 // Compare B |
| 2040 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoB, &runti
meTypeInfoB); err != nil { | 2040 if err := compareTwoGoObjects(c.expectedRuntimeTypeInfoB, &runti
meTypeInfoB); err != nil { |
| 2041 t.Errorf("case %d B:\n%s", i, err.Error()) | 2041 t.Errorf("case %d B:\n%s", i, err.Error()) |
| 2042 } | 2042 } |
| 2043 } | 2043 } |
| 2044 } | 2044 } |
| 2045 | 2045 |
| 2046 func deserializeRuntimeTypeInfo(base64String string) mojom_types.RuntimeTypeInfo
{ |
| 2047 compressedBytes, err := base64.StdEncoding.DecodeString(base64String) |
| 2048 if err != nil { |
| 2049 panic(fmt.Sprintf("Error while unencoding runtimeTypeInfo: %s",
err.Error())) |
| 2050 } |
| 2051 reader, err2 := gzip.NewReader(bytes.NewBuffer(compressedBytes)) |
| 2052 if err2 != nil { |
| 2053 panic(fmt.Sprintf("Error while decompressing runtimeTypeInfo: %s
", err.Error())) |
| 2054 } |
| 2055 uncompressedBytes, err3 := ioutil.ReadAll(reader) |
| 2056 if err3 != nil { |
| 2057 panic(fmt.Sprintf("Error while decompressing runtimeTypeInfo: %s
", err2.Error())) |
| 2058 } |
| 2059 if err = reader.Close(); err != nil { |
| 2060 panic(fmt.Sprintf("Error while decompressing runtimeTypeInfo: %s
", err.Error())) |
| 2061 } |
| 2062 decoder := bindings.NewDecoder(uncompressedBytes, nil) |
| 2063 runtimeTypeInfo := mojom_types.RuntimeTypeInfo{} |
| 2064 runtimeTypeInfo.Decode(decoder) |
| 2065 return runtimeTypeInfo |
| 2066 } |
| 2067 |
| 2046 // compareTwoGoObjects compares |expected| and |actual| and returns a non-nil | 2068 // compareTwoGoObjects compares |expected| and |actual| and returns a non-nil |
| 2047 // error if they are not deeply equal. The error message contains a human-readab
le | 2069 // error if they are not deeply equal. The error message contains a human-readab
le |
| 2048 // string containing a deep-print of expected and actual along with the substrin
gs | 2070 // string containing a deep-print of expected and actual along with the substrin
gs |
| 2049 // starting from the first character where they differ. | 2071 // starting from the first character where they differ. |
| 2050 func compareTwoGoObjects(expected interface{}, actual interface{}) error { | 2072 func compareTwoGoObjects(expected interface{}, actual interface{}) error { |
| 2051 if !reflect.DeepEqual(expected, actual) { | 2073 if !reflect.DeepEqual(expected, actual) { |
| 2052 // Note(rudominer) The myfmt package is a local modification of
the fmt package | 2074 // Note(rudominer) The myfmt package is a local modification of
the fmt package |
| 2053 // that does a deep printing that follows pointers for up to 50
levels. | 2075 // that does a deep printing that follows pointers for up to 50
levels. |
| 2054 // Thus expectedString and actualString should contain enough in
formation to | 2076 // Thus expectedString and actualString should contain enough in
formation to |
| 2055 // precisely capture the structure of expected and actual. | 2077 // precisely capture the structure of expected and actual. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2071 } | 2093 } |
| 2072 return fmt.Errorf("*****\nexpected=\n*****\n%q\n*****\na
ctual=\n*****\n%q\n*****\n"+ | 2094 return fmt.Errorf("*****\nexpected=\n*****\n%q\n*****\na
ctual=\n*****\n%q\n*****\n"+ |
| 2073 "match failed at position %d: expected=\n*****\n
%q\n******\nactual=\n*****\n%q\n******\n", | 2095 "match failed at position %d: expected=\n*****\n
%q\n******\nactual=\n*****\n%q\n******\n", |
| 2074 expectedString, actualString, diffPos, mismatchE
xpected, mismatchActual) | 2096 expectedString, actualString, diffPos, mismatchE
xpected, mismatchActual) |
| 2075 } else { | 2097 } else { |
| 2076 return fmt.Errorf("expected != actual but the two printe
d equal.") | 2098 return fmt.Errorf("expected != actual but the two printe
d equal.") |
| 2077 } | 2099 } |
| 2078 } | 2100 } |
| 2079 return nil | 2101 return nil |
| 2080 } | 2102 } |
| OLD | NEW |