/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2018 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.cactoos.map;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.cactoos.Scalar;
import org.cactoos.iterator.Repeated;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
 * Test case for {@link MapOf}.
 *
 * @author Yegor Bugayenko (yegor256@gmail.com)
 * @version $Id$
 * @since 0.4
 * @checkstyle JavadocMethodCheck (500 lines)
 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 */
public final class MapOfTest {

    @Test
    public void behavesAsMap() {
        MatcherAssert.assertThat(
            "Can't behave as a map",
            new MapNoNulls<>(
                new MapOf<Integer, Integer>(
                    new MapEntry<>(0, -1),
                    new MapEntry<>(1, 1)
                )
            ),
            new BehavesAsMap<>(0, 1)
        );
    }

    @Test
    public void convertsIterableToMap() {
        MatcherAssert.assertThat(
            "Can't convert iterable to map",
            new MapOf<Integer, String>(
                new MapEntry<>(0, "hello, "),
                new MapEntry<>(1, "world!")
            ),
            Matchers.hasEntry(
                Matchers.equalTo(0),
                Matchers.startsWith("hello")
            )
        );
    }

    @Test
    public void sensesChangesInMap() throws Exception {
        final AtomicInteger size = new AtomicInteger(2);
        final Map<Integer, Integer> map = new MapOf<>(
            () -> new Repeated<>(
                size.incrementAndGet(), () -> new MapEntry<>(
                    new SecureRandom().nextInt(),
                    1
                )
            )
        );
        MatcherAssert.assertThat(
            "Can't sense the changes in the underlying map",
            map.size(),
            Matchers.not(Matchers.equalTo(map.size()))
        );
    }

    @Test
    public void createsMapWithFunctions() {
        MatcherAssert.assertThat(
            "Can't create a map with functions as values",
            new MapOf<Integer, Scalar<Boolean>>(
                new MapEntry<>(0, () -> true),
                new MapEntry<>(
                    1,
                    () -> {
                        throw new IOException("oops");
                    }
                )
            ),
            Matchers.hasKey(0)
        );
    }

    @Test
    public void string() {
        MatcherAssert.assertThat(
            "Can't convert to string",
            new MapOf<Integer, Integer>(
                new MapEntry<>(-1, 0),
                new MapEntry<>(1, 2)
            ).toString(),
            Matchers.equalTo("-1=0, 1=2")
        );
    }
}

@@ -3598,17 +3598,27 @@
 ic void 
-s
+integersToS
 tring() 
@@ -3680,16 +3680,32 @@
 convert 
+map of integers 
 to strin
@@ -3881,16 +3881,17 @@
 qualTo(%22
+%7B
 -1=0, 1=
@@ -3891,16 +3891,904 @@
 1=0, 1=2
+%7D%22)%0A        );%0A    %7D%0A%0A    @Test%0A    public void mapsToString() %7B%0A        MatcherAssert.assertThat(%0A            %22Can't convert map op maps to string%22,%0A            new MapOf%3CInteger, Map%3CString, String%3E%3E(%0A                new MapEntry%3C%3E(-1, new MapOf%3C%3E(%0A                    new MapEntry%3C%3E(%22first%22, %22second%22),%0A                    new MapEntry%3C%3E(%224%22, %227%22)%0A                )),%0A                new MapEntry%3C%3E(1, new MapOf%3C%3E(%0A                    new MapEntry%3C%3E(%22green%22, %22red%22),%0A                    new MapEntry%3C%3E(%222.7%22, %223.1%22)%0A                ))%0A            ).toString(),%0A            Matchers.equalTo(%22%7B-1=%7B4=7, first=second%7D, 1=%7Bgreen=red, 2.7=3.1%7D%7D%22)%0A        );%0A    %7D%0A%0A    @Test%0A    public void emptyToString() %7B%0A        MatcherAssert.assertThat(%0A            %22Can't convert empty map to string%22,%0A            new MapOf%3CInteger, Map%3CString, String%3E%3E().toString(),%0A            Matchers.equalTo(%22%7B%7D
 %22)%0A     

add tests