swiftgemtext

[ACTIVE] gemtext parsing in swift
git clone git://git.figbert.com/swiftgemtext.git
Log | Files | Refs | README

SwiftGemtextTests.swift (4269B)


      1 import XCTest
      2 @testable import SwiftGemtext
      3 
      4 final class SwiftGemtextTests: XCTestCase {
      5     func testSingleLine() {
      6         let source = "This is an example line"
      7         let run = Gemtext().parse(source)
      8         XCTAssertEqual([LineType.Text(source)], run)
      9     }
     10     
     11     func testPlainGemini() {
     12         let source = """
     13         # Heading
     14 
     15         Lorem ipsum dolor sit amet.
     16 
     17         => https://figbert.com/ figbert.com (clearnet)
     18         => gemini://figbert.com/ figbert.com (gemini)
     19         => https://news.ycombinator.com/ Hacker News
     20         """
     21         let manual = [
     22             LineType.Heading(1, "Heading"),
     23             LineType.EmptyLine,
     24             LineType.Text("Lorem ipsum dolor sit amet."),
     25             LineType.EmptyLine,
     26             LineType.Link(URL(string: "https://figbert.com/")!, "figbert.com (clearnet)"),
     27             LineType.Link(URL(string: "gemini://figbert.com/")!, "figbert.com (gemini)"),
     28             LineType.Link(URL(string: "https://news.ycombinator.com/")!, "Hacker News"),
     29         ]
     30         let run = Gemtext().parse(source)
     31         XCTAssertEqual(manual, run)
     32     }
     33     
     34     func testRelativeURLs() {
     35         let source = """
     36         Zounds!
     37 
     38         => gemini://figbert.com/ wow its a capsule
     39         => / same
     40         => /a-page i have a few of these
     41         => https://news.ycombinator.com/ Hacker News
     42         """
     43         let base = URL(string: "gemini://figbert.com")
     44 
     45         let manual = [
     46             LineType.Text("Zounds!"),
     47             LineType.EmptyLine,
     48             LineType.Link(URL(string: "gemini://figbert.com/")!, "wow its a capsule"),
     49             LineType.Link(URL(string: "/", relativeTo: base)!, "same"),
     50             LineType.Link(URL(string: "/a-page", relativeTo: base)!, "i have a few of these"),
     51             LineType.Link(URL(string: "https://news.ycombinator.com/")!, "Hacker News"),
     52         ]
     53         let run = Gemtext().parse(source, url: base)
     54         XCTAssertEqual(manual, run)
     55     }
     56 
     57     func testComplexGemini() {
     58         let source = """
     59         # One
     60         ## Two
     61         ### Three
     62 
     63         ```
     64         (?<=\\.\\/IssuerIcons\\/).*(?=\\.png)
     65         ```
     66 
     67         ```python
     68         while True:
     69             print("example")
     70         ```
     71 
     72         Lorem ipsum dolor sit amet.
     73 
     74         * Point
     75         > Quote
     76 
     77         => https://google.com/ Google
     78         =>    /log/ This one's relative!
     79         =>gemini://figbert.com/
     80         =>	gopher://mozz.us/
     81         """
     82         let manual = [
     83             LineType.Heading(1, "One"),
     84             LineType.Heading(2, "Two"),
     85             LineType.Heading(3, "Three"),
     86             LineType.EmptyLine,
     87             LineType.PreformattedText("(?<=\\.\\/IssuerIcons\\/).*(?=\\.png)", nil),
     88             LineType.EmptyLine,
     89             LineType.PreformattedText("while True:\r\n    print(\"example\")", "python"),
     90             LineType.EmptyLine,
     91             LineType.Text("Lorem ipsum dolor sit amet."),
     92             LineType.EmptyLine,
     93             LineType.UnorderedList("Point"),
     94             LineType.Quote("Quote"),
     95             LineType.EmptyLine,
     96             LineType.Link(URL(string: "https://google.com/")!, "Google"),
     97             LineType.Link(URL(string: "/log/")!, "This one's relative!"),
     98             LineType.Link(URL(string: "gemini://figbert.com/")!, nil),
     99             LineType.Link(URL(string: "gopher://mozz.us/")!, nil),
    100         ]
    101         XCTAssertEqual(manual, Gemtext().parse(source))
    102     }
    103 
    104     func testUnicodeURLs() {
    105         let source = """
    106         => gemini://ostov-larion.srht.site/articles/педо.gmi
    107         => gemini://basnja.ru/fables/babrius/лошадь-и-осел-r5858/ hello
    108         """
    109         let run = Gemtext().parse(source)
    110         XCTAssertEqual([
    111             LineType.Link(URL(string: "gemini://ostov-larion.srht.site/articles/%D0%BF%D0%B5%D0%B4%D0%BE.gmi")!, nil),
    112             LineType.Link(URL(string: "gemini://basnja.ru/fables/babrius/%D0%BB%D0%BE%D1%88%D0%B0%D0%B4%D1%8C-%D0%B8-%D0%BE%D1%81%D0%B5%D0%BB-r5858/")!, "hello"),
    113         ], run)
    114     }
    115 
    116     static var allTests = [
    117         ("testSingleLine", testSingleLine),
    118         ("testPlainGemini", testPlainGemini),
    119         ("testRelativeURLs", testRelativeURLs),
    120         ("testComplexGemini", testComplexGemini),
    121         ("testUnicodeURLs", testUnicodeURLs),
    122     ]
    123 }