/*
 * Copyright 2016 DiffPlug
 *
 * 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.diffplug.spotless.npm;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.diffplug.common.collect.ImmutableMap;
import com.diffplug.spotless.ResourceHarness;

public class SimpleJsonWriterTest extends ResourceHarness {

	private SimpleJsonWriter jsonWriter = new SimpleJsonWriter();

	@Test
	public void itWritesAValidEmptyObject() {
		assertThat(jsonWriter.toJsonString().replaceAll("\\s", ""), equalTo("{}"));
	}

	@Test
	public void itWritesABooleanProperty() {
		jsonWriter.put("mybool", true);
		assertThat(jsonWriter.toJsonString(), equalTo("{\n    \"mybool\": true\n}"));
	}

	@Test
	public void itWritesAStringProperty() {
		jsonWriter.put("mystring", "stringvalue");
		assertThat(jsonWriter.toJsonString(), equalTo("{\n    \"mystring\": \"stringvalue\"\n}"));
	}

	@Test
	public void itWritesAnInteger() {
		jsonWriter.put("myint", 7);
		assertThat(jsonWriter.toJsonString(), equalTo("{\n    \"myint\": 7\n}"));
	}

	@Test(expected = IllegalArgumentException.class)
	public void itFailsOnUnsupportedObject() {
		jsonWriter.put("anyobj", new Object());
		fail("should not be accepted");
	}

	@Test
	public void itHandlesSeveralOptionsSimultaneously() {
		jsonWriter.putAll(ImmutableMap.of("mystring", "stringvalue", "intvalue", 1));
		assertThat(jsonWriter.toJsonString(), equalTo("{\n    \"mystring\": \"stringvalue\",\n    \"intvalue\": 1\n}"));
	}

	@Test
	public void itWritesToFile() throws IOException {
		jsonWriter.put("mystring", "stringvalue");
		final File file = newFile("target.json");
		jsonWriter.toJsonFile(file);
		assertFile(file).hasContent("{\n    \"mystring\": \"stringvalue\"\n}");
	}
}

@@ -13,16 +13,21 @@
 ght 2016
+-2020
  DiffPlu
@@ -648,71 +648,98 @@
 org.
-hamcrest.CoreMatchers.equalTo;%0Aimport static org.junit.Assert.*
+assertj.core.api.Assertions.assertThat;%0Aimport static org.assertj.core.api.Assertions.fail
 ;%0A%0Ai
@@ -1142,19 +1142,21 @@
 %5Cs%22, %22%22)
-, e
+).isE
 qualTo(%22
@@ -1159,17 +1159,16 @@
 To(%22%7B%7D%22)
-)
 ;%0A%09%7D%0A%0A%09@
@@ -1278,35 +1278,37 @@
 r.toJsonString()
-, e
+).isE
 qualTo(%22%7B%5Cn    %5C
@@ -1323,25 +1323,24 @@
 %22: true%5Cn%7D%22)
-)
 ;%0A%09%7D%0A%0A%09@Test
@@ -1456,35 +1456,37 @@
 r.toJsonString()
-, e
+).isE
 qualTo(%22%7B%5Cn    %5C
@@ -1514,25 +1514,24 @@
 value%5C%22%5Cn%7D%22)
-)
 ;%0A%09%7D%0A%0A%09@Test
@@ -1626,35 +1626,37 @@
 r.toJsonString()
-, e
+).isE
 qualTo(%22%7B%5Cn    %5C
@@ -1667,25 +1667,24 @@
 nt%5C%22: 7%5Cn%7D%22)
-)
 ;%0A%09%7D%0A%0A%09@Test
@@ -2031,19 +2031,21 @@
 String()
-, e
+).isE
 qualTo(%22
@@ -2107,17 +2107,16 @@
 : 1%5Cn%7D%22)
-)
 ;%0A%09%7D%0A%0A%09@

Replace deprecated hamcrest stuff with assertj.