<commit_before>package fi.vincit.jmobster.processor.frameworks.backbone;

import fi.vincit.jmobster.processor.FieldValueConverter;
import fi.vincit.jmobster.processor.ModelProcessor;
import fi.vincit.jmobster.processor.languages.javascript.writer.JavaScriptWriter;
import fi.vincit.jmobster.processor.model.Model;
import fi.vincit.jmobster.util.itemprocessor.ItemStatuses;
import fi.vincit.jmobster.util.writer.DataWriter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

public class BackboneModelProcessorTest {

    public static final String COMMENT = "/*\n * Auto-generated file\n */\n";
    public static final String FIRST_LINE = "var Models = {\n";
    public static final String TEST_MODEL_NAME = "TestModel";
    private DataWriter writer;
    private MockWriter mockWriter;
    @Mock private FieldValueConverter fieldValueConverter;
    @Mock private ValidatorProcessor validatorProcessor;
    @Mock private ModelProcessor<JavaScriptWriter> valueProcessor;

    @Before
    public void init() {
        mockWriter();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testStartProcessing() throws Exception {
        ModelProcessor processor = createProcessor();

        processor.startProcessing(ItemStatuses.firstAndLast());

        verify(writer).write(COMMENT);
        verify(writer).write(FIRST_LINE);
    }

    @Test
    public void testStartProcessingWithCustomNamespace() throws Exception {
        BackboneModelProcessor processor = createProcessor();
        processor.setNamespaceName("Foo");

        processor.startProcessing(ItemStatuses.firstAndLast());

        verify(writer).write(COMMENT);
        verify(writer).write("var Foo = {\n");
    }

    @Test
    public void testStartProcessingWithCustomComment() throws Exception {
        BackboneModelProcessor processor = createProcessor();
        processor.setStartComment("/* Foo */");

        processor.startProcessing(ItemStatuses.firstAndLast());

        verify(writer).write("/* Foo */\n");
        verify(writer).write(FIRST_LINE);
    }

    @Test
    public void testEndProcessing() throws Exception {
        ModelProcessor processor = createProcessor();

        processor.endProcessing(ItemStatuses.firstAndLast());

        verify(writer).write("};\n");
        assertThat(writer.isOpen(), is(false));
    }

    @Test
    public void testProcessFirstModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.first());

        InOrder order = inOrder(writer, validatorProcessor, valueProcessor);
        order.verify(writer).write(TEST_MODEL_NAME);
        order.verify(writer).write(": Backbone.Model.extend({\n");

        order.verify(writer).write("validation");
        order.verify(writer).write(": ");
        verify(validatorProcessor).startProcessing(ItemStatuses.first());
        verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(writer).write("defaults");
        order.verify(writer).write(": ");
        verify(valueProcessor).startProcessing(ItemStatuses.last());
        verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(valueProcessor).endProcessing(ItemStatuses.last());

        order.verify(writer).write("}),\n");
    }

    @Test
    public void testProcessMiddleModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.notFirstNorLast());

        InOrder order = inOrder(writer, validatorProcessor, valueProcessor);
        order.verify(writer).write(TEST_MODEL_NAME);
        order.verify(writer).write(": Backbone.Model.extend({\n");

        order.verify(writer).write("validation");
        order.verify(writer).write(": ");
        verify(validatorProcessor).startProcessing(ItemStatuses.first());
        verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(writer).write("defaults");
        order.verify(writer).write(": ");
        verify(valueProcessor).startProcessing(ItemStatuses.last());
        verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(valueProcessor).endProcessing(ItemStatuses.last());

        order.verify(writer).write("}),\n");
    }

    @Test
    public void testProcessLastModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.last());

        InOrder order = inOrder(writer, validatorProcessor, valueProcessor);
        order.verify(writer).write(TEST_MODEL_NAME);
        order.verify(writer).write(": Backbone.Model.extend({\n");

        order.verify(writer).write("validation");
        order.verify(writer).write(": ");
        verify(validatorProcessor).startProcessing(ItemStatuses.first());
        verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(writer).write("defaults");
        order.verify(writer).write(": ");
        verify(valueProcessor).startProcessing(ItemStatuses.last());
        verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        verify(valueProcessor).endProcessing(ItemStatuses.last());

        order.verify(writer).write("})\n");
    }

    private Model mockModel() {
        Model model = mock(Model.class);
        when(model.getName()).thenReturn(TEST_MODEL_NAME);
        return model;
    }


    private BackboneModelProcessor createProcessor() {
        BackboneModelProcessor processor = new BackboneModelProcessor(
                mockWriter,
                fieldValueConverter,
                validatorProcessor,
                valueProcessor);

        return processor;
    }

    private MockWriter mockWriter() {
        writer = mock(DataWriter.class);
        mockWriter = new MockWriter(writer);

        when(writer.indent()).thenReturn(writer);
        when(writer.write(anyString())).thenReturn(writer);
        when(writer.write(anyString(), anyString(), anyBoolean())).thenReturn(writer);
        when(writer.writeLine(anyString())).thenReturn(writer);
        when(writer.indentBack()).thenReturn(writer);
        when(writer.writeLine(anyString(), anyString(), anyBoolean())).thenReturn(writer);


        return new MockWriter(writer);
    }

}<commit_msg>Change BackboneModelProcessorTest to use strings<commit_after>package fi.vincit.jmobster.processor.frameworks.backbone;

import fi.vincit.jmobster.processor.FieldValueConverter;
import fi.vincit.jmobster.processor.ModelProcessor;
import fi.vincit.jmobster.processor.languages.javascript.writer.JavaScriptWriter;
import fi.vincit.jmobster.processor.model.Model;
import fi.vincit.jmobster.util.itemprocessor.ItemStatuses;
import fi.vincit.jmobster.util.writer.StringBufferWriter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

public class BackboneModelProcessorTest {

    public static final String COMMENT = "/*\n * Auto-generated file\n */\n";
    public static final String FIRST_LINE = "var Models = {\n";
    public static final String TEST_MODEL_NAME = "TestModel";
    private StringBufferWriter writer;
    @Mock private FieldValueConverter fieldValueConverter;
    @Mock private ValidatorProcessor validatorProcessor;
    @Mock private ModelProcessor<JavaScriptWriter> valueProcessor;

    @Before
    public void init() {
        writer = new StringBufferWriter();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testStartProcessing() throws Exception {
        ModelProcessor processor = createProcessor();

        processor.startProcessing(ItemStatuses.firstAndLast());

        assertThat(writer.toString(), is(
                "/*\n" +
                " * Auto-generated file\n" +
                " */\n" +
                "var Models = {\n"
        ));
    }

    @Test
    public void testStartProcessingWithCustomNamespace() throws Exception {
        BackboneModelProcessor processor = createProcessor();
        processor.setNamespaceName("Foo");

        processor.startProcessing(ItemStatuses.firstAndLast());

        assertThat(writer.toString(), is(
                "/*\n" +
                " * Auto-generated file\n" +
                " */\n" +
                "var Foo = {\n"
        ));
    }

    @Test
    public void testStartProcessingWithCustomComment() throws Exception {
        BackboneModelProcessor processor = createProcessor();
        processor.setStartComment("/* Foo */");

        processor.startProcessing(ItemStatuses.firstAndLast());

        assertThat(writer.toString(), is(
                "/* Foo */\n" +
                "var Models = {\n"
        ));
    }

    @Test
    public void testEndProcessing() throws Exception {
        ModelProcessor processor = createProcessor();

        processor.endProcessing(ItemStatuses.firstAndLast());

        assertThat(writer.toString(), is("};\n"));
        assertThat(writer.isOpen(), is(false));
    }

    @Test
    public void testProcessFirstModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.first());

        InOrder order = inOrder(validatorProcessor, valueProcessor);
        order.verify(validatorProcessor).startProcessing(ItemStatuses.first());
        order.verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(valueProcessor).startProcessing(ItemStatuses.last());
        order.verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(valueProcessor).endProcessing(ItemStatuses.last());

        assertThat(writer.toString(), is(
                "TestModel: Backbone.Model.extend({\n" +
                "    validation: defaults: \n" +
                "})," +
                "\n"
        ));
    }

    @Test
    public void testProcessMiddleModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.notFirstNorLast());

        InOrder order = inOrder(validatorProcessor, valueProcessor);
        order.verify(validatorProcessor).startProcessing(ItemStatuses.first());
        order.verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(valueProcessor).startProcessing(ItemStatuses.last());
        order.verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(valueProcessor).endProcessing(ItemStatuses.last());

        assertThat(writer.toString(), is(
                "TestModel: Backbone.Model.extend({\n" +
                "    validation: defaults: \n" +
                "})," +
                "\n"
        ));
    }

    @Test
    public void testProcessLastModel() throws Exception{
        ModelProcessor processor = createProcessor();

        Model model = mockModel();
        processor.processModel(model, ItemStatuses.last());

        InOrder order = inOrder(validatorProcessor, valueProcessor);
        order.verify(validatorProcessor).startProcessing(ItemStatuses.first());
        order.verify(validatorProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(validatorProcessor).endProcessing(ItemStatuses.first());

        order.verify(valueProcessor).startProcessing(ItemStatuses.last());
        order.verify(valueProcessor).processModel(model, ItemStatuses.firstAndLast());
        order.verify(valueProcessor).endProcessing(ItemStatuses.last());

        assertThat(writer.toString(), is(
                "TestModel: Backbone.Model.extend({\n" +
                "    validation: defaults: \n" +
                "})" +
                "\n"));
    }

    private Model mockModel() {
        Model model = mock(Model.class);
        when(model.getName()).thenReturn(TEST_MODEL_NAME);
        return model;
    }


    private BackboneModelProcessor createProcessor() {
        BackboneModelProcessor processor = new BackboneModelProcessor(
                writer,
                fieldValueConverter,
                validatorProcessor,
                valueProcessor);

        return processor;
    }

}
{'fraction_non_alphanumeric': 0.11842404919152813, 'fraction_numerical': 0.0, 'mean_word_length': 3.253793994187924, 'pattern_counts': {'":': 9, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6082715', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3336, 'n_words': 569}