package com.github.brunodles.simplepreferences.lib.parsers;

import android.content.SharedPreferences;

import com.github.brunodles.simplepreferences.lib.Property;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Created by bruno on 16/12/15.
 */
public class StringParserTest {

    public static class WhenHaveSavedValue {
        private StringParser stringParser;
        private SharedPreferences preferences;
        private SampleObject object;
        private Field field;

        @Before
        public void setup() throws NoSuchFieldException {
            stringParser = new StringParser();
            preferences = mock(SharedPreferences.class);
            when(preferences.getString("field", null)).thenReturn("test");
            object = new SampleObject();
            field = SampleObject.class.getField("field");
        }

        @Test
        public void shouldDeserialize() throws IllegalAccessException {
            stringParser.load(preferences, field, object, field.getAnnotation(Property.class));
            Assert.assertEquals("Should be equals", "test", object.field);
        }

        private static class SampleObject {
            @Property public String field;
        }
    }

    public static class WhenDontHaveSavedValueButHaveDefault {
        private StringParser stringParser;
        private SharedPreferences preferences;
        private SampleObject object;
        private Field field;

        @Before
        public void setup() throws NoSuchFieldException {
            stringParser = new StringParser();
            preferences = mock(SharedPreferences.class);
            when(preferences.getString("field", "defaultValue")).thenReturn("defaultValue");
            object = new SampleObject();
            field = SampleObject.class.getField("field");
        }

        @Test
        public void shouldDeserialize() throws IllegalAccessException {
            stringParser.load(preferences, field, object, field.getAnnotation(Property.class));
            Assert.assertEquals("Should be equals", "defaultValue", object.field);
        }

        private static class SampleObject {
            @Property public String field = "defaultValue";
        }
    }
}
@@ -162,40 +162,8 @@
 y;%0A%0A
-import junit.framework.Assert;%0A%0A
 impo
@@ -262,28 +262,156 @@
 org.
-mockito.Mockito.mock
+junit.Assert.assertEquals;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify
 ;%0Aim
@@ -1124,34 +1124,42 @@
  void should
-Deserializ
+ReturnTheSavedValu
 e() throws I
@@ -1282,39 +1282,32 @@
 ));%0A            
-Assert.
 assertEquals(%22Sh
@@ -1338,32 +1338,100 @@
  object.field);%0A
+            verify(preferences, times(1)).getString(%22field%22, null);%0A
         %7D%0A%0A     
@@ -2174,18 +2174,30 @@
 ould
-Deserializ
+ReturnTheDefaultValue3
 e() 
@@ -2336,23 +2336,16 @@
         
-Assert.
 assertEq
@@ -2392,32 +2392,110 @@
  object.field);%0A
+            verify(preferences, times(1)).getString(%22field%22, %22defaultValue%22);%0A
         %7D%0A%0A     

Fix the test to verify mocked class