package org.jabref.model.search.rules;

import java.util.EnumSet;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Test case for GrammarBasedSearchRuleTest.
 */
public class GrammarBasedSearchRuleTest {

    @Test
    void applyRuleMatchesSingleTermWithRegex() {
        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.of(SearchRules.SearchFlags.CASE_SENSITIVE, SearchRules.SearchFlags.REGULAR_EXPRESSION));

        String query = "M[a-z]+e";
        assertTrue(searchRule.validateSearchStrings(query));
        assertTrue(searchRule.applyRule(query, makeBibtexEntry()));
    }

    @Test
    void applyRuleDoesNotMatchSingleTermWithRegex() {
        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.of(SearchRules.SearchFlags.CASE_SENSITIVE, SearchRules.SearchFlags.REGULAR_EXPRESSION));

        String query = "M[0-9]+e";
        assertTrue(searchRule.validateSearchStrings(query));
        assertFalse(searchRule.applyRule(query, makeBibtexEntry()));
    }

    public BibEntry makeBibtexEntry() {
        return new BibEntry(StandardEntryType.InCollection)
                .withCitationKey("shields01")
                .withField(StandardField.TITLE, "Marine finfish larviculture in Europe")
                .withField(StandardField.YEAR, "2001")
                .withField(StandardField.AUTHOR, "Kevin Shields");
    }
}

@@ -207,16 +207,55 @@
 yType;%0A%0A
+import org.junit.jupiter.api.Disabled;%0A
 import o
@@ -1298,24 +1298,2553 @@
 )));%0A    %7D%0A%0A
+    @Test%0A    void searchRuleOfDocumentationMatches() %7B%0A        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.of(SearchRules.SearchFlags.REGULAR_EXPRESSION));%0A%0A        String query = %22(author = miller or title%7Ckeywords = %5C%22image processing%5C%22) and not author = brown%22;%0A        assertTrue(searchRule.validateSearchStrings(query));%0A        assertTrue(searchRule.applyRule(query, new BibEntry()%0A                .withCitationKey(%22key%22)%0A                .withField(StandardField.KEYWORDS, %22image processing%22)));%0A        assertFalse(searchRule.applyRule(query, new BibEntry()%0A                .withCitationKey(%22key%22)%0A                .withField(StandardField.AUTHOR, %22Sam Brown%22)%0A                .withField(StandardField.KEYWORDS, %22image processing%22)));%0A    %7D%0A%0A    @Disabled%0A    @Test%0A    void searchForAnyFieldWorks() %7B%0A        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.of(SearchRules.SearchFlags.REGULAR_EXPRESSION));%0A%0A        String query = %22anyfield:fruit%22;%0A        assertTrue(searchRule.validateSearchStrings(query));%0A        assertTrue(searchRule.applyRule(query, new BibEntry()%0A                .withField(StandardField.KEYWORDS, %22fruit%22)));%0A    %7D%0A%0A    @Disabled%0A    @Test%0A    void searchForAnyKeywordWorks() %7B%0A        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.of(SearchRules.SearchFlags.REGULAR_EXPRESSION));%0A%0A        String query = %22anykeyword:apple%22;%0A        assertTrue(searchRule.validateSearchStrings(query));%0A        assertTrue(searchRule.applyRule(query, new BibEntry()%0A                .withField(StandardField.KEYWORDS, %22apple%22)));%0A        assertFalse(searchRule.applyRule(query, new BibEntry()%0A                .withField(StandardField.KEYWORDS, %22pineapple%22)));%0A    %7D%0A%0A    @Test%0A    void searchForCitationKeyWorks() %7B%0A        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.noneOf(SearchRules.SearchFlags.class));%0A        String query = %22citationkey==miller2005%22;%0A        assertTrue(searchRule.validateSearchStrings(query));%0A        assertTrue(searchRule.applyRule(query, new BibEntry()%0A                .withCitationKey(%22miller2005%22)));%0A    %7D%0A%0A    @Test%0A    void searchForThesisEntryTypeWorks() %7B%0A        GrammarBasedSearchRule searchRule = new GrammarBasedSearchRule(EnumSet.noneOf(SearchRules.SearchFlags.class));%0A        String query = %22entrytype=thesis%22;%0A        assertTrue(searchRule.validateSearchStrings(query));%0A        assertTrue(searchRule.applyRule(query, new BibEntry(StandardEntryType.PhdThesis)));%0A    %7D%0A%0A
     public B

Add doc tests (#8242)