/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.time;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.text.StringUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class TimeMetricTest {

    @Test
    public void testStop() throws InterruptedException {
        TimeMetric tm = new TimeMetric(TimeUnit.MILLISECONDS);
        tm.start();
        
        Thread.sleep(50);
        assertTimerText(50, "ms", tm);
        assertEquals(50.0, tm.stop(), 25.0);
        
        Thread.sleep(50);
        assertTimerText(100, "ms", tm);
        assertEquals(100.0, tm.stop(), 25.0);
        
        assertThrows(NoSuchElementException.class, ()->{
            System.out.printf("%d%n", tm.stop());
        });
    }
    
    private static void assertTimerText(int value, String unit, TimeMetric tm) {
        String text = tm.toString();
        assertTrue("Invalid unit: " + text, text.endsWith("ms"));
        int number = Integer.parseInt(StringUtils.before(text, "ms"));
        assertEquals(value, number, 25.0);
    }
    
}
