logseq2gemtext/tests/ArticleParserTest.php

111 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace LogseqGem\Tests;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use LogseqGem\ArticleParser;
class ArticleParserTest extends TestCase {
private vfsStreamDirectory $root;
private ArticleParser $parser;
public function setUp(): void {
$this->parser = new ArticleParser;
$this->root = vfsStream::setup();
}
private function setArticleText(string $text) {
vfsStream::newFile('article.md')
->withContent($text)
->at($this->root);
}
public function testConvertSimple() {
$this->setArticleText("- This is a test line.");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame('This is a test line.', $this->parser->convert());
}
public function testConvertLink() {
$this->setArticleText("- [foo](bar)");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame('=> bar foo', $this->parser->convert());
}
public function testMultiParagraph() {
$this->setArticleText("- Block 1\n- Block 2");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("Block 1\n\nBlock 2", $this->parser->convert());
}
public function testEmpty() {
$this->setArticleText("-");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("", $this->parser->convert());
}
public function testInterleavedParagraphsAndLinks() {
$this->setArticleText("- Block 1\n- [foo](bar)\n- Block 2");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("Block 1\n\n=> bar foo\n\nBlock 2", $this->parser->convert());
}
public function testSequentialLinks() {
$this->setArticleText("\n- [foo](bar)\n- [bar](foo)");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("=> bar foo\n=> foo bar", $this->parser->convert());
}
public function testFrontmatter() {
$this->setArticleText("foo:: bar\nanother:: prop\n\n- first line");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("first line", $this->parser->convert());
$this->assertSame(["foo" => "bar", "another" => "prop"], $this->parser->getProperties());
}
public function testDraftIsNotPublished() {
$this->setArticleText("status:: draft\n\n- my article");
$this->parser->load(vfsStream::url('root/article.md'));
$this->parser->convert();
$this->assertFalse($this->parser->isPublished());
}
public function testMissingStatusIsNotPublished() {
$this->setArticleText("status:: draft\n\n- my article");
$this->parser->load(vfsStream::url('root/article.md'));
$this->parser->convert();
$this->assertFalse($this->parser->isPublished());
}
public function testPublished() {
$this->setArticleText("status:: published\n\n- my article");
$this->parser->load(vfsStream::url('root/article.md'));
$this->parser->convert();
$this->assertTrue($this->parser->isPublished());
}
public function testNewlineBetweenLinks() {
$this->setArticleText("\n- [foo](bar)\n-\n- [bar](foo)");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("=> bar foo\n\n=> foo bar", $this->parser->convert());
}
public function testList() {
$this->setArticleText("- para\n\t- item 1\n\t- item 2\n- out");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("para\n\n* item 1\n* item 2\n\nout", $this->parser->convert());
}
public function testBlockquote() {
$this->setArticleText("- before\n- > this is a quote\n it spans 2 lines\n- more");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("before\n\n> this is a quote\n> it spans 2 lines\n\nmore", $this->parser->convert());
}
}