aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/proto2/proto2_test.html
blob: 1e4e3ce3cc07dd665bd2efcd66c46da035733b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.proto2</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.proto2');
  goog.require('proto2.TestAllTypes');
</script>
</head>
<body>
<script>

function testAssertEquals() {
  var assertProto2Equals = goog.testing.proto2.assertEquals;
  assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes);
  assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes, 'oops');

  var ex = assertThrows(goog.partial(assertProto2Equals,
      new proto2.TestAllTypes, new proto2.TestAllTypes.NestedMessage));
  assertEquals(
      'Message type mismatch: TestAllTypes != TestAllTypes.NestedMessage',
      ex.message);

  var message = new proto2.TestAllTypes;
  message.setOptionalInt32(1);
  ex = assertThrows(goog.partial(assertProto2Equals,
      new proto2.TestAllTypes, message));
  assertEquals('optional_int32 should not be present', ex.message);

  ex = assertThrows(goog.partial(assertProto2Equals,
      new proto2.TestAllTypes, message, 'oops'));
  assertEquals('oops\noptional_int32 should not be present', ex.message);
}

function testFindDifferences_EmptyMessages() {
  assertEquals('', goog.testing.proto2.findDifferences_(
      new proto2.TestAllTypes, new proto2.TestAllTypes, ''));
}

function testFindDifferences_FieldNotPresent() {
  var message = new proto2.TestAllTypes;
  message.setOptionalInt32(0);
  var empty = new proto2.TestAllTypes;
  assertEquals('optional_int32 should not be present',
      goog.testing.proto2.findDifferences_(empty, message, ''));
  assertEquals('optional_int32 should be present',
      goog.testing.proto2.findDifferences_(message, empty, ''));
  assertEquals('path/optional_int32 should be present',
      goog.testing.proto2.findDifferences_(message, empty, 'path'));
}

function testFindDifferences_IntFieldDiffers() {
  var message1 = new proto2.TestAllTypes;
  message1.setOptionalInt32(1);
  var message2 = new proto2.TestAllTypes;
  message2.setOptionalInt32(2);
  assertEquals('optional_int32 should be 1, but was 2',
      goog.testing.proto2.findDifferences_(message1, message2, ''));
}

function testFindDifferences_NestedIntFieldDiffers() {
  var message1 = new proto2.TestAllTypes;
  var nested1 = new proto2.TestAllTypes.NestedMessage();
  nested1.setB(1);
  message1.setOptionalNestedMessage(nested1);
  var message2 = new proto2.TestAllTypes;
  var nested2 = new proto2.TestAllTypes.NestedMessage();
  nested2.setB(2);
  message2.setOptionalNestedMessage(nested2);
  assertEquals('optional_nested_message/b should be 1, but was 2',
      goog.testing.proto2.findDifferences_(message1, message2, ''));
}

function testFindDifferences_RepeatedFieldLengthDiffers() {
  var message1 = new proto2.TestAllTypes;
  message1.addRepeatedInt32(1);
  var message2 = new proto2.TestAllTypes;
  message2.addRepeatedInt32(1);
  message2.addRepeatedInt32(2);
  assertEquals('repeated_int32 should have 1 items, but has 2',
      goog.testing.proto2.findDifferences_(message1, message2, ''));
}

function testFindDifferences_RepeatedFieldItemDiffers() {
  var message1 = new proto2.TestAllTypes;
  message1.addRepeatedInt32(1);
  var message2 = new proto2.TestAllTypes;
  message2.addRepeatedInt32(2);
  assertEquals('repeated_int32[0] should be 1, but was 2',
      goog.testing.proto2.findDifferences_(message1, message2, ''));
}

function testFindDifferences_RepeatedNestedMessageDiffers() {
  var message1 = new proto2.TestAllTypes;
  var nested1 = new proto2.TestAllTypes.NestedMessage();
  nested1.setB(1);
  message1.addRepeatedNestedMessage(nested1);
  var message2 = new proto2.TestAllTypes;
  var nested2 = new proto2.TestAllTypes.NestedMessage();
  nested2.setB(2);
  message2.addRepeatedNestedMessage(nested2);
  assertEquals('repeated_nested_message[0]/b should be 1, but was 2',
      goog.testing.proto2.findDifferences_(message1, message2, ''));
}

</script>
</body>
</html>