/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Dmitry Dolzhenko
 *
 * 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 io.github.dsdolzhenko.bencoding;


import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

public class BListTest {
    @Test
    public void listsWithTheSameValuesShouldBeEqual() throws Exception {
        final BList<BValue> firstValue = new BList<>();
        final BList<BValue> secondValue = new BList<>();

        firstValue.add(new BInteger(1));
        firstValue.add(new BString("string".getBytes()));

        secondValue.add(new BInteger(1));
        secondValue.add(new BString("string".getBytes()));

        assertEquals(firstValue, secondValue);
    }

    @Test
    public void listsWithDifferentValuesShouldNotBeEqual() throws Exception {
        final BList<BValue> firstValue = new BList<>();
        final BList<BValue> secondValue = new BList<>();

        firstValue.add(new BString("string".getBytes()));
        firstValue.add(new BInteger(100));

        secondValue.add(new BInteger(1));
        secondValue.add(new BString("string".getBytes()));

        assertNotEquals(firstValue, secondValue);
    }

    @Test
    public void listShouldBeEncodedCorrectly() throws Exception {
        final BList<BValue> values = new BList<>();

        values.add(new BString("string".getBytes()));
        values.add(new BInteger(100));

        assertEquals(values.encode(), "l6:stringi100ee");
    }

    @Test
    public void emptyListShouldBeEncodedCorrectly() throws Exception {
        final BList<BValue> values = new BList<>();

        assertEquals(values.encode(), "le");
    }

    @Test
    public void emptyListShouldBeDecodedSuccessfully() throws Exception {
        assertEquals(new BList<>(), BList.decode(new BReader("le")));
    }

    @Test
    public void simpleListShouldBeDecodedSuccessfully() throws Exception {
        final BList<BValue> list = new BList<>();

        list.add(new BString("string".getBytes()));
        list.add(new BInteger(1));

        assertEquals(list, BList.decode(new BReader("l6:stringi1e")));
    }
}
