From f07ef0fe22e6af27cd012990a07c288ab8913327 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Sun, 16 May 2021 14:35:22 +0100 Subject: [PATCH] Start adding test for Oik_attachment_contents. Issue #14 --- libs/class-oik-attachment-contents.php | 5 +- ...est-libs-class-oik-attachment-contents.php | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/test-libs-class-oik-attachment-contents.php diff --git a/libs/class-oik-attachment-contents.php b/libs/class-oik-attachment-contents.php index 823fdf1..1c6a0a9 100644 --- a/libs/class-oik-attachment-contents.php +++ b/libs/class-oik-attachment-contents.php @@ -1,6 +1,6 @@ contents_array from $this->content */ function get_contents() { - $content = str_replace( "
\n", "\n", $this->content ); + $content = $this->content; $content = str_replace( '\n', "\n", $content ); + $content = str_replace( "
\n", "\n", $content ); $content = rtrim( $content ); bw_trace2( $content, "content", false ); $content_array = explode( "\n", $content ); diff --git a/tests/test-libs-class-oik-attachment-contents.php b/tests/test-libs-class-oik-attachment-contents.php new file mode 100644 index 0000000..80394db --- /dev/null +++ b/tests/test-libs-class-oik-attachment-contents.php @@ -0,0 +1,80 @@ +assertStringEndsWith('libs/class-oik-attachment-contents.php', $lib ); + $this->assertEquals( '0.0.1', CLASS_OIK_ATTACHMENT_CONTENTS_INCLUDED ); + } + + function test_load_lib() { + $this->load_lib(); + $this->assertTrue( class_exists( 'Oik_attachment_contents')); + } + + /** + * Once the library has been loaded can we assume that we can call the constructor? + * How do we know that test_load_lib() has been called? + */ + function test__construct() { + $oik_attachment_contents = new Oik_attachment_contents(); + $this->assertInstanceOf( 'Oik_attachment_contents', $oik_attachment_contents ); + } + + function test_get_content() { + $oik_attachment_contents = new Oik_attachment_contents(); + $content = $oik_attachment_contents->get_content( null, null ); + $this->AssertNull( $content ); + $content = $oik_attachment_contents->get_content( null, "" ); + $this->AssertNull( $content ); + $content = $oik_attachment_contents->get_content( null, 'A,B,C\n1,2,3' ); + $this->AssertEquals( 'A,B,C\n1,2,3', $content ); + } + + function test_get_contents_array() { + $oik_attachment_contents = new Oik_attachment_contents(); + //$content = $oik_attachment_contents->get_content( null, null ); + $contents_array = $oik_attachment_contents->get_contents_array( null, null ); + $this->assertEquals([ '' ], $contents_array ); + //$content = $oik_attachment_contents->get_content( null, "" ); + $contents_array = $oik_attachment_contents->get_contents_array( null, ""); + $this->assertEquals([ '' ], $contents_array ); + //$content = $oik_attachment_contents->get_content( null, 'A,B,C\n1,2,3' ); + $contents_array = $oik_attachment_contents->get_contents_array( null, 'A,B,C\n1,2,3'); + $this->AssertEquals( [ 'A,B,C', '1,2,3'], $contents_array ); + $contents_array = $oik_attachment_contents->get_contents_array( null, "A,B,C\n1,2,3"); + $this->AssertEquals( [ 'A,B,C', '1,2,3'], $contents_array ); + //
has to be immediately before the new line. + // The code allows for \n appearing in a string enclosed in single quotes. - which isn't actually a new line character + $contents_array = $oik_attachment_contents->get_contents_array( null, 'A,B,C
\n1,2,3'); + $this->AssertEquals( [ 'A,B,C', '1,2,3'], $contents_array ); + $contents_array = $oik_attachment_contents->get_contents_array( null, "A,B,C
\n1,2,3"); + $this->AssertEquals( [ 'A,B,C', '1,2,3'], $contents_array ); + + } + + + +}