package org.wikipedia.page;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.wikipedia.dataclient.WikiSite;

import java.util.Locale;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.wikipedia.page.Namespace.FILE;
import static org.wikipedia.page.Namespace.MAIN;
import static org.wikipedia.page.Namespace.MEDIA;
import static org.wikipedia.page.Namespace.SPECIAL;
import static org.wikipedia.page.Namespace.TALK;

@RunWith(RobolectricTestRunner.class) public class NamespaceTest {
    private static Locale PREV_DEFAULT_LOCALE;

    @BeforeClass public static void setUp() {
        PREV_DEFAULT_LOCALE = Locale.getDefault();
        Locale.setDefault(Locale.ENGLISH);
    }

    @AfterClass public static void tearDown() {
        Locale.setDefault(PREV_DEFAULT_LOCALE);
    }

    @Test public void testOf() {
        assertThat(Namespace.of(SPECIAL.code()), is(SPECIAL));
    }

    @Test public void testFromLegacyStringMain() {
        //noinspection deprecation
        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode("test"), null), is(MAIN));
    }

    @Test public void testFromLegacyStringFile() {
        //noinspection deprecation
        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode("he"), "קובץ"), is(FILE));
    }

    @Test public void testFromLegacyStringSpecial() {
        //noinspection deprecation
        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode("lez"), "Служебная"), is(SPECIAL));
    }

    @Test public void testFromLegacyStringTalk() {
        //noinspection deprecation
        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode("en"), "stringTalk"), is(TALK));
    }

    @Test public void testCode() {
        assertThat(MAIN.code(), is(0));
        assertThat(TALK.code(), is(1));
    }

    @Test public void testSpecial() {
        assertThat(SPECIAL.special(), is(true));
        assertThat(MAIN.special(), is(false));
    }

    @Test public void testMain() {
        assertThat(MAIN.main(), is(true));
        assertThat(TALK.main(), is(false));
    }

    @Test public void testFile() {
        assertThat(FILE.file(), is(true));
        assertThat(MAIN.file(), is(false));
    }

    @Test public void testTalkNegative() {
        assertThat(MEDIA.talk(), is(false));
        assertThat(SPECIAL.talk(), is(false));
    }

    @Test public void testTalkZero() {
        assertThat(MAIN.talk(), is(false));
    }

    @Test public void testTalkOdd() {
        assertThat(TALK.talk(), is(true));
    }

    @Test public void testToLegacyStringMain() {
        //noinspection deprecation
        assertThat(MAIN.toLegacyString(), nullValue());
    }

    @Test public void testToLegacyStringNonMain() {
        //noinspection deprecation
        assertThat(TALK.toLegacyString(), is("Talk"));
    }
}

@@ -640,16 +640,119 @@
 ce.TALK;
+%0Aimport static org.wikipedia.page.Namespace.USER;%0Aimport static org.wikipedia.page.Namespace.USER_TALK;
 %0A%0A@RunWi
@@ -1967,14 +1967,8 @@
 ), %22
-string
 Talk
@@ -1974,16 +1974,738 @@
 k%22), is(
+TALK));%0A        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode(%22ru%22), %22%D0%9E%D0%B1%D1%81%D1%83%D0%B6%D0%B4%D0%B5%D0%BD%D0%B8%D0%B5%22), is(TALK));%0A    %7D%0A%0A    @Test public void testFromLegacyStringUser() %7B%0A        //noinspection deprecation%0A        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode(%22en%22), %22User%22), is(USER));%0A        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode(%22af%22), %22Gebruiker%22), is(USER));%0A    %7D%0A%0A    @Test public void testFromLegacyStringUserTalk() %7B%0A        //noinspection deprecation%0A        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode(%22en%22), %22User Talk%22), is(USER_TALK));%0A        assertThat(Namespace.fromLegacyString(WikiSite.forLanguageCode(%22vi%22), %22Th%E1%BA%A3o lu%E1%BA%ADn Th%C3%A0nh vi%C3%AAn%22), is(USER_
 TALK));%0A

Fix tests.