Skip to content

Commit

Permalink
EasyTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaeva Daria committed Oct 12, 2015
1 parent 5f9eff0 commit f224529
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.fizteh.fivt.students.mamaevads.moduletests.library;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

//dontreadme
//tweet.getCreatedAt().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(
// LocalDateTime curDate = LocalDateTime.now();

public class TimeHandler {
public static final int TWO_MINUTES = 120;

static String getType(LocalDateTime myDayTime, LocalDateTime nowDayTime) {
LocalDateTime tweetDate = myDayTime;
LocalDateTime curDate = nowDayTime;
if (ChronoUnit.SECONDS.between(tweetDate, curDate) < TWO_MINUTES) {
return "только что";
} else if (ChronoUnit.HOURS.between(tweetDate, curDate) == 0) {
long between = ChronoUnit.MINUTES.between(tweetDate, curDate);
return between + " " + WordForms.getForm(between, WordForms.MINUTES_FORMS) + " назад";
} else if (ChronoUnit.DAYS.between(tweetDate, curDate) <= 1) {
if (tweetDate.getDayOfMonth() == nowDayTime.getDayOfMonth()) {
long between = ChronoUnit.HOURS.between(tweetDate, curDate);
return between + " " + WordForms.getForm(between, WordForms.HOUR_FORMS) + " назад";
} else {
return "вчера";
}
} else {
long between = ChronoUnit.DAYS.between(tweetDate, curDate);
return between + " " + WordForms.getForm(between, WordForms.DAYS_FORMS) + " назад";
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.fizteh.fivt.students.mamaevads.moduletests.library;

public class WordForms {
public static final String[] HOUR_FORMS = {"час", "часа", "часов"};
public static final String[] MINUTES_FORMS = {"минуту", "минуты", "минут"};
public static final String[] DAYS_FORMS = {"день", "дня", "дней"};
public static final String[] RE_FORMS = {"ретвит", "ретвита", "ретвитов"};

public static final int ONE = 1;
public static final int TEN = 10;
public static final int TWO_TEN = 20;
public static final int TEN_ONE = 11;
public static final int TWO = 2;
public static final int TEN_TEN = 100;
public static final int TWO_TWO = 4;

static String getForm(long number, String[] words) {
if (number == ONE || (number > TWO_TEN && number % TEN == ONE && number % TEN_TEN != TEN_ONE)) {
return words[0];
} else if ((number % TEN_TEN < TEN || number % TEN_TEN > TWO_TEN)
&& number % TEN >= TWO && number % TEN <= TWO_TWO) {
return words[1];
} else {
return words[2];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.fizteh.fivt.students.mamaevads.moduletests.library;

import junit.framework.TestCase;
import org.junit.Test;
import junit.framework.Assert;


import java.time.LocalDateTime;
import java.time.Month;


public class TimeHandlerTest extends TestCase {

@Test
public void testGetType() throws Exception {
LocalDateTime timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 15, 0, 4);
LocalDateTime timeTwo = LocalDateTime.of(2010, Month.JANUARY, 20, 15, 2, 3);
assertEquals("только что", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 23, 59, 25);
timeTwo = LocalDateTime.of(2010, Month.JANUARY, 21, 00, 00, 30);
assertEquals("только что", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 15, 1, 1);
timeTwo = LocalDateTime.of(2010, Month.JANUARY, 20, 15, 3, 3);
assertEquals("2 минуты назад", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 15, 1, 1);
timeTwo = LocalDateTime.of(2010, Month.JANUARY, 20, 16, 1, 0);
assertEquals("59 минут назад", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 13, 1, 1);
timeTwo = LocalDateTime.of(2010, Month.JANUARY, 20, 16, 1, 0);
assertEquals("2 часа назад", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2010, Month.JANUARY, 20, 23, 1, 1);
timeTwo = LocalDateTime.of(2010, Month.JANUARY, 21, 02, 1, 0);
assertEquals("вчера", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2012, Month.FEBRUARY, 26, 00, 1, 1);
timeTwo = LocalDateTime.of(2012, Month.FEBRUARY, 27, 23, 59, 59);
assertEquals("вчера", TimeHandler.getType(timeOne,timeTwo));
timeOne = LocalDateTime.of(2012, Month.FEBRUARY, 25, 00, 1, 1);
timeTwo = LocalDateTime.of(2012, Month.FEBRUARY, 27, 23, 59, 59);
assertEquals("2 дня назад", TimeHandler.getType(timeOne, timeTwo));
timeOne = LocalDateTime.of(2012, Month.FEBRUARY, 28, 00, 1, 1); //Leap year
timeTwo = LocalDateTime.of(2012, Month.MARCH, 01, 23, 59, 59);
assertEquals("2 дня назад", TimeHandler.getType(timeOne, timeTwo));
timeOne = LocalDateTime.of(2012, Month.FEBRUARY, 29, 1, 1, 1);
timeTwo = LocalDateTime.of(2012, Month.MARCH, 01, 22, 59, 59);
assertEquals("вчера", TimeHandler.getType(timeOne, timeTwo));
timeOne = LocalDateTime.of(2012, Month.FEBRUARY, 28, 1, 1, 1);
timeTwo = LocalDateTime.of(2012, Month.MARCH, 06, 22, 59, 59);
assertEquals("7 дней назад", TimeHandler.getType(timeOne, timeTwo));
timeOne = LocalDateTime.of(2011, Month.FEBRUARY, 28, 1, 1, 1);
timeTwo = LocalDateTime.of(2011, Month.MARCH, 06, 22, 59, 59);
assertEquals("6 дней назад", TimeHandler.getType(timeOne, timeTwo));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.fizteh.fivt.students.mamaevads.moduletests.library;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.Test;

public class WordFormsTest extends TestCase {

@Test
public void testGetForm() throws Exception {
Assert.assertEquals("день", WordForms.getForm(1, WordForms.DAYS_FORMS));
Assert.assertEquals("минуту", WordForms.getForm(1, WordForms.MINUTES_FORMS));
Assert.assertEquals("час", WordForms.getForm(1, WordForms.HOUR_FORMS));
Assert.assertEquals("дня", WordForms.getForm(2, WordForms.DAYS_FORMS));
Assert.assertEquals("минуты", WordForms.getForm(2, WordForms.MINUTES_FORMS));
Assert.assertEquals("часа", WordForms.getForm(2, WordForms.HOUR_FORMS));
Assert.assertEquals("дня", WordForms.getForm(4, WordForms.DAYS_FORMS));
Assert.assertEquals("минуты", WordForms.getForm(4, WordForms.MINUTES_FORMS));
Assert.assertEquals("часа", WordForms.getForm(4, WordForms.HOUR_FORMS));
Assert.assertEquals("дней", WordForms.getForm(12, WordForms.DAYS_FORMS));
Assert.assertEquals("минут", WordForms.getForm(12, WordForms.MINUTES_FORMS));
Assert.assertEquals("часов", WordForms.getForm(12, WordForms.HOUR_FORMS));
Assert.assertEquals("дней", WordForms.getForm(14, WordForms.DAYS_FORMS));
Assert.assertEquals("минут", WordForms.getForm(14, WordForms.MINUTES_FORMS));
Assert.assertEquals("часов", WordForms.getForm(14, WordForms.HOUR_FORMS));
Assert.assertEquals("дня", WordForms.getForm(22, WordForms.DAYS_FORMS));
Assert.assertEquals("минуты", WordForms.getForm(22, WordForms.MINUTES_FORMS));
Assert.assertEquals("часа", WordForms.getForm(22, WordForms.HOUR_FORMS));
Assert.assertEquals("дня", WordForms.getForm(24, WordForms.DAYS_FORMS));
Assert.assertEquals("минуты", WordForms.getForm(24, WordForms.MINUTES_FORMS));
Assert.assertEquals("часа", WordForms.getForm(24, WordForms.HOUR_FORMS));
Assert.assertEquals("дней", WordForms.getForm(11, WordForms.DAYS_FORMS));
Assert.assertEquals("минут", WordForms.getForm(11, WordForms.MINUTES_FORMS));
Assert.assertEquals("часов", WordForms.getForm(11, WordForms.HOUR_FORMS));
Assert.assertEquals("дней", WordForms.getForm(111, WordForms.DAYS_FORMS));
Assert.assertEquals("минут", WordForms.getForm(111, WordForms.MINUTES_FORMS));
Assert.assertEquals("часов", WordForms.getForm(111, WordForms.HOUR_FORMS));
Assert.assertEquals("день", WordForms.getForm(131, WordForms.DAYS_FORMS));
Assert.assertEquals("минуту", WordForms.getForm(131, WordForms.MINUTES_FORMS));
Assert.assertEquals("час", WordForms.getForm(131, WordForms.HOUR_FORMS));
Assert.assertEquals("дней", WordForms.getForm(113, WordForms.DAYS_FORMS));
Assert.assertEquals("минут", WordForms.getForm(113, WordForms.MINUTES_FORMS));
Assert.assertEquals("часов", WordForms.getForm(113, WordForms.HOUR_FORMS));
}
}

0 comments on commit f224529

Please sign in to comment.