// Copyright 2020 Google LLC
//
// 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 com.google.api.generator.engine.ast;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;

public class StringObjectValueTest {

  private StringObjectValue valueSpecialChar;

  @Test
  public void writeStringObjectValue() {
    StringObjectValue s = StringObjectValue.builder().setValue("test").build();
    assertThat(s.value()).isEqualTo("\"test\"");
    assertThat(s.type()).isEqualTo(TypeNode.STRING);
  }

  @Test
  public void writeStringObjectValue_escapeCharacter() {
    valueSpecialChar = StringObjectValue.withValue("\" \t \\ \b \r \f \n '");
    String expected = "\"\\\" \\t \\\\ \\b \\r \\f \\n '\"";
    assertThat(valueSpecialChar.value()).isEqualTo(expected);
    assertThat(valueSpecialChar.type()).isEqualTo(TypeNode.STRING);
  }

  @Test
  public void writeStringObjectValue_specialCharacter() {
    valueSpecialChar = StringObjectValue.withValue("Tom said: \"Hi!\"; \n");
    String expected = "\"Tom said: \\\"Hi!\\\"; \\n\"";
    assertThat(valueSpecialChar.value()).isEqualTo(expected);
    assertThat(valueSpecialChar.type()).isEqualTo(TypeNode.STRING);
  }

  @Test
  public void writeStringObjectValue_specialCharacterComment() {
    valueSpecialChar =
        StringObjectValue.withValue("Service comment may include special characters: <>&\"`'@");
    String expected = "\"Service comment may include special characters: <>&\\\"`'@\"";
    assertThat(valueSpecialChar.value()).isEqualTo(expected);
    assertThat(valueSpecialChar.type()).isEqualTo(TypeNode.STRING);
  }

  @Test
  public void writeStringObjectValue_USPunctuation() {
    valueSpecialChar =
        StringObjectValue.withValue("US Punctuation, one of !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
    String expected = "\"US Punctuation, one of !\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~\"";
    assertThat(valueSpecialChar.value()).isEqualTo(expected);
    assertThat(valueSpecialChar.type()).isEqualTo(TypeNode.STRING);
  }

  @Test
  public void writeStringObjectValue_htmlCharacterComment() {
    valueSpecialChar = StringObjectValue.withValue("&nbsp; &#40 &#91 &ndash; &gt;:&lt;");
    String expected = "\"&nbsp; &#40 &#91 &ndash; &gt;:&lt;\"";
    assertThat(valueSpecialChar.value()).isEqualTo(expected);
    assertThat(valueSpecialChar.type()).isEqualTo(TypeNode.STRING);
  }
}

@@ -683,16 +683,61 @@
 ertThat;
+%0Aimport static org.junit.Assert.assertThrows;
 %0A%0Aimport
@@ -1067,32 +1067,237 @@
 e.STRING);%0A  %7D%0A%0A
+  @Test%0A  public void writeInvalieStringObjectValue() %7B%0A    assertThrows(%0A        NullPointerException.class,%0A        () -%3E %7B%0A          StringObjectValue.builder().setValue(null).build();%0A        %7D);%0A  %7D%0A%0A
   @Test%0A  public

add invalid null string test