package com.librato.metrics;

import junit.framework.Assert;
import junit.framework.TestCase;

import java.util.regex.Pattern;

public class SourceInformationTest extends TestCase {

    public void testHandlesNullRegex() throws Exception {
        SourceInformation info = SourceInformation.from(null, "foo");
        Assert.assertNull(info.source);
        Assert.assertEquals("foo", info.name);
    }

    public void testExtractsSource() throws Exception {
        Pattern pattern = Pattern.compile("^([^\\.]+)\\.");
        SourceInformation info = SourceInformation.from(pattern, "foo.bar");
        Assert.assertEquals("foo", info.source);
        Assert.assertEquals("bar", info.name);
    }

    public void testRequiresAMatchingGroup() throws Exception {
        Pattern pattern = Pattern.compile("^[^\\.]+\\.");
        SourceInformation info = SourceInformation.from(pattern, "foo.bar");
        Assert.assertNull(info.source);
        Assert.assertEquals("foo.bar", info.name);
    }

    public void testPassesThroughNonMatchingMetricNames() throws Exception {
        Pattern pattern = Pattern.compile("^([^\\.]+)\\.");
        SourceInformation info = SourceInformation.from(pattern, "foo-bar");
        Assert.assertNull(info.source);
        Assert.assertEquals("foo-bar", info.name);
    }
}

@@ -1301,11 +1301,342 @@
 );%0A    %7D
+%0A%0A    public void testExtractsGroupingInTheMiddleOfAnExpression() throws Exception %7B%0A        Pattern pattern = Pattern.compile(%22%5E--(.*?)--%22);%0A        SourceInformation info = SourceInformation.from(pattern, %22--foo--bar.baz%22);%0A        Assert.assertEquals(%22foo%22, info.source);%0A        Assert.assertEquals(%22bar.baz%22, info.name);%0A    %7D
 %0A%7D%0A

Test for discarding text before and after the source match