/**
 * @license
 * Copyright The Closure Library Authors.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @fileoverview Test helpers to compare goog.proto2.Messages.
 */

goog.setTestOnly('goog.testing.proto2');
goog.provide('goog.testing.proto2');

goog.require('goog.proto2.Message');
goog.require('goog.proto2.ObjectSerializer');
goog.require('goog.testing.asserts');


/**
 * Compares two goog.proto2.Message instances of the same type.
 * @param {!goog.proto2.Message} expected First message.
 * @param {!goog.proto2.Message} actual Second message.
 * @param {string} path Path to the messages.
 * @return {string} A string describing where they differ. Empty string if they
 *     are equal.
 * @private
 */
goog.testing.proto2.findDifferences_ = function(expected, actual, path) {
  'use strict';
  var fields = expected.getDescriptor().getFields();
  for (var i = 0; i < fields.length; i++) {
    var field = fields[i];
    var newPath = (path ? path + '/' : '') + field.getName();

    if (expected.has(field) && !actual.has(field)) {
      return newPath + ' should be present';
    }
    if (!expected.has(field) && actual.has(field)) {
      return newPath + ' should not be present';
    }

    if (expected.has(field)) {
      var isComposite = field.isCompositeType();

      if (field.isRepeated()) {
        var expectedCount = expected.countOf(field);
        var actualCount = actual.countOf(field);
        if (expectedCount != actualCount) {
          return newPath + ' should have ' + expectedCount + ' items, ' +
              'but has ' + actualCount;
        }

        for (var j = 0; j < expectedCount; j++) {
          var expectedItem = expected.get(field, j);
          var actualItem = actual.get(field, j);
          if (isComposite) {
            var itemDiff = goog.testing.proto2.findDifferences_(
                /** @type {!goog.proto2.Message} */ (expectedItem),
                /** @type {!goog.proto2.Message} */ (actualItem),
                newPath + '[' + j + ']');
            if (itemDiff) {
              return itemDiff;
            }
          } else {
            if (expectedItem != actualItem) {
              return newPath + '[' + j + '] should be ' + expectedItem +
                  ', but was ' + actualItem;
            }
          }
        }
      } else {
        var expectedValue = expected.get(field);
        var actualValue = actual.get(field);
        if (isComposite) {
          var diff = goog.testing.proto2.findDifferences_(
              /** @type {!goog.proto2.Message} */ (expectedValue),
              /** @type {!goog.proto2.Message} */ (actualValue), newPath);
          if (diff) {
            return diff;
          }
        } else {
          if (expectedValue != actualValue) {
            return newPath + ' should be ' + expectedValue + ', but was ' +
                actualValue;
          }
        }
      }
    }
  }

  return '';
};


/**
 * Compares two goog.proto2.Message objects. Gives more readable output than
 * assertObjectEquals on mismatch.
 * @param {!goog.proto2.Message} expected Expected proto2 message.
 * @param {!goog.proto2.Message} actual Actual proto2 message.
 * @param {string=} opt_failureMessage Failure message when the values don't
 *     match.
 */
goog.testing.proto2.assertEquals = function(
    expected, actual, opt_failureMessage) {
  'use strict';
  var failureSummary = opt_failureMessage || '';
  if (!(expected instanceof goog.proto2.Message) ||
      !(actual instanceof goog.proto2.Message)) {
    goog.testing.asserts.raiseException(
        failureSummary,
        'Bad arguments were passed to goog.testing.proto2.assertEquals');
  }
  if (expected.constructor != actual.constructor) {
    goog.testing.asserts.raiseException(
        failureSummary, 'Message type mismatch: ' +
            expected.getDescriptor().getFullName() + ' != ' +
            actual.getDescriptor().getFullName());
  }
  var diff = goog.testing.proto2.findDifferences_(expected, actual, '');
  if (diff) {
    goog.testing.asserts.raiseException(failureSummary, diff);
  }
};


/**
 * Helper function to quickly build protocol buffer messages from JSON objects.
 * @param {function(new:MessageType)} messageCtor A constructor that
 *     creates a `goog.proto2.Message` subclass instance.
 * @param {!Object} json JSON object which uses field names as keys.
 * @return {MessageType} The deserialized protocol buffer.
 * @template MessageType
 */
goog.testing.proto2.fromObject = function(messageCtor, json) {
  'use strict';
  var serializer = new goog.proto2.ObjectSerializer(
      goog.proto2.ObjectSerializer.KeyOption.NAME);
  var message = new messageCtor;
  serializer.deserializeTo(message, json);
  return message;
};

@@ -802,19 +802,21 @@
 ict';%0A  
-var
+const
  fields 
@@ -862,19 +862,19 @@
 %0A  for (
-var
+let
  i = 0; 
@@ -903,19 +903,21 @@
 ) %7B%0A    
-var
+const
  field =
@@ -932,19 +932,21 @@
 i%5D;%0A    
-var
+const
  newPath
@@ -1243,19 +1243,21 @@
 %7B%0A      
-var
+const
  isCompo
@@ -1321,35 +1321,37 @@
 ed()) %7B%0A        
-var
+const
  expectedCount =
@@ -1380,27 +1380,29 @@
 d);%0A        
-var
+const
  actualCount
@@ -1609,19 +1609,19 @@
    for (
-var
+let
  j = 0; 
@@ -1648,35 +1648,37 @@
 ++) %7B%0A          
-var
+const
  expectedItem = 
@@ -1707,27 +1707,29 @@
 ;%0A          
-var
+const
  actualItem 
@@ -1793,19 +1793,21 @@
         
-var
+const
  itemDif
@@ -2339,19 +2339,21 @@
         
-var
+const
  expecte
@@ -2390,19 +2390,21 @@
         
-var
+const
  actualV
@@ -2466,19 +2466,21 @@
         
-var
+const
  diff = 
@@ -3393,19 +3393,21 @@
 ict';%0A  
-var
+const
  failure
@@ -3951,19 +3951,21 @@
 ;%0A  %7D%0A  
-var
+const
  diff = 
@@ -4559,19 +4559,21 @@
 ict';%0A  
-var
+const
  seriali
@@ -4670,11 +4670,13 @@
 ;%0A  
-var
+const
  mes

Use ES6 const/let more throughout Closure Library. RELNOTES: n/a