github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/lib/text/text_test.go (about)

     1  // Tests for the text package
     2  package text
     3  
     4  import (
     5  	"testing"
     6  )
     7  
     8  type t struct {
     9  	in  string
    10  	out string
    11  }
    12  
    13  var newlineTests = []t{
    14  	{
    15  		in: `mypara
    16  		`,
    17  		out: `<p>mypara</p>`,
    18  	},
    19  }
    20  
    21  // TestConvertNewlines tests newlines -> <p></p>
    22  func TestConvertNewlines(t *testing.T) {
    23  	for _, v := range newlineTests {
    24  		r := ConvertNewlines(v.in)
    25  		if r != v.out {
    26  			t.Fatalf("🔥 Failed to transform newlines\n\twanted:%s\n\tgot:%s\n", v.out, r)
    27  		}
    28  	}
    29  }
    30  
    31  var activateLinksTests = []t{
    32  	{
    33  		in:  `<a href="https://google.com?foo=bar#123">https://google.com</a>`,
    34  		out: `<a href="https://google.com?foo=bar#123">https://google.com</a>`,
    35  	},
    36  	{
    37  		in:  `https://google.com`,
    38  		out: `<a href="https://google.com">https://google.com</a>`,
    39  	},
    40  	{
    41  		in: `🗑🔥
    42  		
    43  		https://google.com`,
    44  		out: `🗑🔥
    45  		
    46  		<a href="https://google.com">https://google.com</a>`,
    47  	},
    48  	{
    49  		in:  ` https://google.com      `,
    50  		out: ` <a href="https://google.com">https://google.com</a>      `,
    51  	},
    52  	{
    53  		in: `  https://news.ycombinator.com/item?id=13213902?foo=bar#fragment
    54      `,
    55  		out: `  <a href="https://news.ycombinator.com/item?id=13213902?foo=bar#fragment">https://news.ycombinator.com/item?id=13213902?foo=bar#fragment</a>
    56      `,
    57  	},
    58  	{
    59  		in:  ` @tester!`,
    60  		out: ` <a href="/u/tester">@tester</a>!`,
    61  	},
    62  	{ // Test medium-style urls with @ usernames
    63  		in:  `https://medium.com/@taylorotwell/measuring-code-complexity-64356da605f9#.wayfi5mch`,
    64  		out: `<a href="https://medium.com/@taylorotwell/measuring-code-complexity-64356da605f9#.wayfi5mch">https://medium.com/@taylorotwell/measuring-code-complexity-64356da605f9#.wayfi5mch</a>`,
    65  	},
    66  	{ // Note this will be escaped when put into the template
    67  		in:  `https://d>medium.com/#.wayfi5mch`,
    68  		out: `<a href="https://d">https://d</a>>medium.com/#.wayfi5mch`,
    69  	},
    70  }
    71  
    72  // TestConvertLinks tests links and usernames are converted
    73  func TestConvertLinks(t *testing.T) {
    74  	for _, v := range activateLinksTests {
    75  		r := ConvertLinks(v.in)
    76  		if r != v.out {
    77  			t.Fatalf("🔥 Failed to transform links\n\twanted:%s\n\tgot:%s\n", v.out, r)
    78  		}
    79  	}
    80  }