Back to Repositories

Testing Page List Parser Implementation in Stirling-PDF

This test suite validates the GeneralUtils class page list parsing functionality in Stirling-PDF, focusing on handling various page number formats and mathematical expressions. The tests cover zero-based and one-based indexing scenarios, along with complex mathematical formulas for page selection.

Test Coverage Overview

The test suite provides comprehensive coverage of page list parsing functionality:
  • Basic ‘all’ and ‘n’ keyword handling
  • Mathematical expressions with n-based formulas
  • Range-based page selection
  • Zero-based vs one-based indexing
  • Complex bracket expressions and consecutive n operations

Implementation Analysis

The testing approach utilizes JUnit Jupiter’s assertion framework to validate page list parsing outcomes. Tests are structured to verify both simple and complex mathematical expressions, employing parameterized inputs to test various scenarios.

Key patterns include systematic validation of mathematical expressions, boundary testing, and comprehensive edge case coverage.

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Static assertion methods from org.junit.jupiter.api.Assertions
  • List comparison for result validation
  • Systematic test method naming convention

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method naming reflecting test scenarios
  • Comprehensive documentation through descriptive assertion messages
  • Systematic coverage of edge cases and boundary conditions
  • Isolated test cases for specific functionality

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/utils/GeneralUtilsTest.java

            
package stirling.software.SPDF.utils;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class GeneralUtilsTest {


    @Test
    void testParsePageListWithAll() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"all"}, 5, false);
        assertEquals(List.of(0, 1, 2, 3, 4), result, "'All' keyword should return all pages.");
    }

    @Test
    void testParsePageListWithAllOneBased() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"all"}, 5, true);
        assertEquals(List.of(1, 2, 3, 4, 5), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFunc() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"n"}, 5, true);
        assertEquals(List.of(1, 2, 3, 4, 5), result, "'n' keyword should return all pages.");
    }

    @Test
    void nFuncAdvanced() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n"}, 9, true);
        //skip 0 as not valid
        assertEquals(List.of(4, 8), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFuncAdvancedZero() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n"}, 9, false);
        //skip 0 as not valid
        assertEquals(List.of(3, 7), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFuncAdvanced2() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n-1"}, 9, true);
        // skip -1 as not valid
        assertEquals(List.of(3, 7), result, "4n-1 should do (0-1), (4-1), (8-1)");
    }

    @Test
    void nFuncAdvanced3() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n+1"}, 9, true);
        assertEquals(List.of(5, 9), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFunc_spaces() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"n + 1"}, 9, true);
        assertEquals(List.of(2, 3, 4, 5, 6, 7, 8, 9), result);
    }

    @Test
    void nFunc_consecutive_Ns_nnn() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"nnn"}, 9, true);
        assertEquals(List.of(1, 8), result);
    }

    @Test
    void nFunc_consecutive_Ns_nn() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"nn"}, 9, true);
        assertEquals(List.of(1, 4, 9), result);
    }

    @Test
    void nFunc_opening_closing_round_brackets() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"(n-1)(n-2)"}, 9, true);
        assertEquals(List.of(2, 6), result);
    }

    @Test
    void nFunc_opening_round_brackets() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"2(n-1)"}, 9, true);
        assertEquals(List.of(2, 4, 6, 8), result);
    }

    @Test
    void nFunc_opening_round_brackets_n() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"n(n-1)"}, 9, true);
        assertEquals(List.of(2, 6), result);
    }

    @Test
    void nFunc_closing_round_brackets() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"(n-1)2"}, 9, true);
        assertEquals(List.of(2, 4, 6, 8), result);
    }

    @Test
    void nFunc_closing_round_brackets_n() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"(n-1)n"}, 9, true);
        assertEquals(List.of(2, 6), result);
    }

    @Test
    void nFunc_function_surrounded_with_brackets() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"(n-1)"}, 9, true);
        assertEquals(List.of(1, 2, 3, 4, 5, 6, 7, 8), result);
    }


    @Test
    void nFuncAdvanced4() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"3+2n"}, 9, true);
        assertEquals(List.of(5, 7, 9), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFuncAdvancedZerobased() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n"}, 9, false);
        assertEquals(List.of(3, 7), result, "'All' keyword should return all pages.");
    }

    @Test
    void nFuncAdvanced2Zerobased() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"4n-1"}, 9, false);
        assertEquals(List.of(2, 6), result, "'All' keyword should return all pages.");
    }

    @Test
    void testParsePageListWithRangeOneBasedOutput() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"1-3"}, 5, true);
        assertEquals(List.of(1, 2, 3), result, "Range should be parsed correctly.");
    }

    @Test
    void testParsePageListWithRangeZeroBaseOutput() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"1-3"}, 5, false);
        assertEquals(List.of(0, 1, 2), result, "Range should be parsed correctly.");
    }


    @Test
    void testParsePageListWithRangeOneBasedOutputFull() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"1,3,7-8"}, 8, true);
        assertEquals(List.of(1, 3, 7, 8), result, "Range should be parsed correctly.");
    }

    @Test
    void testParsePageListWithRangeOneBasedOutputFullOutOfRange() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"1,3,7-8"}, 5, true);
        assertEquals(List.of(1, 3), result, "Range should be parsed correctly.");
    }

    @Test
    void testParsePageListWithRangeZeroBaseOutputFull() {
        List<Integer> result = GeneralUtils.parsePageList(new String[]{"1,3,7-8"}, 8, false);
        assertEquals(List.of(0, 2, 6, 7), result, "Range should be parsed correctly.");
    }
}