SPB Git

Repository Public

APAStyleTests.swift 19.3 KB 197 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocReferencesTests/APAStyleTests.swift3// Description: APA 7 formatting checked against the reference examples published on apastyle.apa.org (pages named in each test).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import DocModel10@testable import DocReferences1112/// Expected strings: https://apastyle.apa.org/style-grammar-guidelines/references/examples/<page> (checked on archived copies of the13/// official pages, 2022–2025). Italics are written between asterisks.14@Suite struct APAStyleTests {15    static let grady = BibEntry(key: "grady2019", type: "article-journal",16        authors: [person("Grady", "Jessica S."), person("Her", "Malina"), person("Moreno", "Geena"), person("Perez", "Catherine"), person("Yelinek", "Jillian")],17        year: 2019, fields: ["title": "Emotions in storybooks: A comparison of storybooks that represent ethnic and racial groups in the United States",18                             "container-title": "Psychology of Popular Media Culture", "volume": "8", "issue": "3", "page": "207-217", "DOI": "10.1037/ppm0000185"])19    static let jackson = BibEntry(key: "jackson2019", type: "book", authors: [person("Jackson", "L. M.")], year: 2019,20        fields: ["title": "The psychology of prejudice: From attitudes to social action", "edition": "2", "publisher": "American Psychological Association",21                 "DOI": "https://doi.org/10.1037/0000168-000"])2223    @Test func journalArticle() {24        // journal-article-references, example 125        #expect(formatted("apa", Self.grady) == "Grady, J. S., Her, M., Moreno, G., Perez, C., & Yelinek, J. (2019). Emotions in storybooks: A comparison of storybooks that represent ethnic and racial groups in the United States. *Psychology of Popular Media Culture*, *8*(3), 207–217. https://doi.org/10.1037/ppm0000185")26    }2728    @Test func journalArticleWithArticleNumber() {29        // journal-article-references, "journal article with an article number"30        let e = BibEntry(key: "jerrentrup2018", type: "article-journal",31            authors: [person("Jerrentrup", "A."), person("Mueller", "T."), person("Glowalla", "U."), person("Herder", "M."), person("Henrichs", "N."),32                      person("Neubauer", "A."), person("Schaefer", "J. R.")],33            year: 2018, fields: ["title": "Teaching medicine with the help of “Dr. House”", "container-title": "PLoS ONE", "volume": "13", "issue": "3",34                                 ReferenceField.articleNumber: "e0193972", "DOI": "10.1371/journal.pone.0193972"])35        #expect(formatted("apa", e) == "Jerrentrup, A., Mueller, T., Glowalla, U., Herder, M., Henrichs, N., Neubauer, A., & Schaefer, J. R. (2018). Teaching medicine with the help of “Dr. House.” *PLoS ONE*, *13*(3), Article e0193972. https://doi.org/10.1371/journal.pone.0193972")36    }3738    @Test func books() {39        // book-references: authored book with DOI and edition; authored book without DOI40        #expect(formatted("apa", Self.jackson) == "Jackson, L. M. (2019). *The psychology of prejudice: From attitudes to social action* (2nd ed.). American Psychological Association. https://doi.org/10.1037/0000168-000")41        let sapolsky = BibEntry(key: "sapolsky2017", type: "book", authors: [person("Sapolsky", "Robert M.")], year: 2017,42            fields: ["title": "Behave: The biology of humans at our best and worst", "publisher": "Penguin Books"])43        #expect(formatted("apa", sapolsky) == "Sapolsky, R. M. (2017). *Behave: The biology of humans at our best and worst*. Penguin Books.")44    }4546    @Test func editedBooks() {47        // book-references: edited book without DOI, edited book with DOI48        let k = BibEntry(key: "kesharwani2020", type: "book", editors: [person("Kesharwani", "P.")], year: 2020,49            fields: ["title": "Nanotechnology based approaches for tuberculosis treatment", "publisher": "Academic Press"])50        #expect(formatted("apa", k) == "Kesharwani, P. (Ed.). (2020). *Nanotechnology based approaches for tuberculosis treatment*. Academic Press.")51        let t = BibEntry(key: "torino2019", type: "book",52            editors: [person("Torino", "G. C."), person("Rivera", "D. P."), person("Capodilupo", "C. M."), person("Nadal", "K. L."), person("Sue", "D. W.")],53            year: 2019, fields: ["title": "Microaggression theory: Influence and implications", "publisher": "John Wiley & Sons", "DOI": "10.1002/9781119466642"])54        #expect(formatted("apa", t) == "Torino, G. C., Rivera, D. P., Capodilupo, C. M., Nadal, K. L., & Sue, D. W. (Eds.). (2019). *Microaggression theory: Influence and implications*. John Wiley & Sons. https://doi.org/10.1002/9781119466642")55    }5657    @Test func chaptersInEditedBooks() {58        // edited-book-chapter-references59        let aron = BibEntry(key: "aron2019", type: "chapter", authors: [person("Aron", "L."), person("Botella", "M."), person("Lubart", "T.")],60            editors: [person("Subotnik", "R. F."), person("Olszewski-Kubilius", "P."), person("Worrell", "F. C.")], year: 2019,61            fields: ["title": "Culinary arts: Talent and their development",62                     "container-title": "The psychology of high performance: Developing human potential into domain-specific talent",63                     "page": "345–359", "publisher": "American Psychological Association", "DOI": "10.1037/0000120-016"])64        #expect(formatted("apa", aron) == "Aron, L., Botella, M., & Lubart, T. (2019). Culinary arts: Talent and their development. In R. F. Subotnik, P. Olszewski-Kubilius, & F. C. Worrell (Eds.), *The psychology of high performance: Developing human potential into domain-specific talent* (pp. 345–359). American Psychological Association. https://doi.org/10.1037/0000120-016")65        let dillard = BibEntry(key: "dillard2020", type: "chapter", authors: [person("Dillard", "J. P.")],66            editors: [person("Oliver", "M. B."), person("Raney", "A. A."), person("Bryant", "J.")], year: 2020,67            fields: ["title": "Currents in the study of persuasion", "container-title": "Media effects: Advances in theory and research", "edition": "4th",68                     "page": "115--129", "publisher": "Routledge"])69        #expect(formatted("apa", dillard) == "Dillard, J. P. (2020). Currents in the study of persuasion. In M. B. Oliver, A. A. Raney, & J. Bryant (Eds.), *Media effects: Advances in theory and research* (4th ed., pp. 115–129). Routledge.")70    }7172    @Test func governmentReportWithGroupAuthor() {73        // report-government-agency-references74        let e = BibEntry(key: "nci2019", type: "report", authors: [org("National Cancer Institute")], year: 2019,75            fields: ["title": "Taking time: Support for people with cancer", "genre": "NIH Publication", "number": "18-2059",76                     "publisher": "U.S. Department of Health and Human Services, National Institutes of Health",77                     "URL": "https://www.cancer.gov/publications/patient-education/takingtime.pdf"])78        #expect(formatted("apa", e) == "National Cancer Institute. (2019). *Taking time: Support for people with cancer* (NIH Publication No. 18-2059). U.S. Department of Health and Human Services, National Institutes of Health. https://www.cancer.gov/publications/patient-education/takingtime.pdf")79    }8081    @Test func dissertations() {82        // unpublished-dissertation-references83        let harris = BibEntry(key: "harris2014", type: "thesis", authors: [person("Harris", "L.")], year: 2014,84            fields: ["title": "Instructional leadership perceptions and practices of elementary school leaders", "genre": "Unpublished doctoral dissertation",85                     "publisher": "University of Virginia"])86        #expect(formatted("apa", harris) == "Harris, L. (2014). *Instructional leadership perceptions and practices of elementary school leaders* [Unpublished doctoral dissertation]. University of Virginia.")87        // published-dissertation-references88        let kabir = BibEntry(key: "kabir2016", type: "thesis", authors: [person("Kabir", "J. M.")], year: 2016,89            fields: ["title": "Factors influencing customer satisfaction at a fast food hamburger chain: The relationship between customer satisfaction and customer loyalty",90                     "number": "10169573", "genre": "Doctoral dissertation", "publisher": "Wilmington University", "archive": "ProQuest Dissertations & Theses Global"])91        #expect(formatted("apa", kabir) == "Kabir, J. M. (2016). *Factors influencing customer satisfaction at a fast food hamburger chain: The relationship between customer satisfaction and customer loyalty* (Publication No. 10169573) [Doctoral dissertation, Wilmington University]. ProQuest Dissertations & Theses Global.")92    }9394    @Test func webpages() {95        // webpage-website-references96        let who = BibEntry(key: "who2018", type: "webpage", authors: [org("World Health Organization")], year: 2018, month: 5, day: 24,97            fields: ["title": "The top 10 causes of death", "URL": "https://www.who.int/news-room/fact-sheets/detail/the-top-10-causes-of-death"])98        #expect(formatted("apa", who) == "World Health Organization. (2018, May 24). *The top 10 causes of death*. https://www.who.int/news-room/fact-sheets/detail/the-top-10-causes-of-death")99        let bologna = BibEntry(key: "bologna2019", type: "webpage", authors: [person("Bologna", "C.")], year: 2019, month: 10, day: 31,100            fields: ["title": "Why some people with anxiety love watching horror movies", "container-title": "HuffPost",101                     "URL": "https://www.huffpost.com/entry/anxiety-love-watching-horror-movies_l_5d277587e4b02a5a5d57b59e"])102        #expect(formatted("apa", bologna) == "Bologna, C. (2019, October 31). *Why some people with anxiety love watching horror movies*. HuffPost. https://www.huffpost.com/entry/anxiety-love-watching-horror-movies_l_5d277587e4b02a5a5d57b59e")103        let census = BibEntry(key: "census", type: "webpage", authors: [org("U.S. Census Bureau")],104            fields: ["title": "U.S. and world population clock", "publisher": "U.S. Department of Commerce", "accessed": "2020-01-09",105                     "URL": "https://www.census.gov/popclock/"])106        #expect(formatted("apa", census) == "U.S. Census Bureau. (n.d.). *U.S. and world population clock*. U.S. Department of Commerce. Retrieved January 9, 2020, from https://www.census.gov/popclock/")107        let nimh = BibEntry(key: "nimh2018", type: "webpage", authors: [org("National Institute of Mental Health")], year: 2018, month: 7,108            fields: ["title": "Anxiety disorders", "publisher": "U.S. Department of Health and Human Services, National Institutes of Health",109                     "URL": "https://www.nimh.nih.gov/health/topics/anxiety-disorders/index.shtml"])110        #expect(formatted("apa", nimh) == "National Institute of Mental Health. (2018, July). *Anxiety disorders*. U.S. Department of Health and Human Services, National Institutes of Health. https://www.nimh.nih.gov/health/topics/anxiety-disorders/index.shtml")111    }112113    @Test func newspaperArticle() {114        // newspaper-article-references115        let e = BibEntry(key: "carey2019", type: "article-newspaper", authors: [person("Carey", "B.")], year: 2019, month: 3, day: 22,116            fields: ["title": "Can we get better at forgetting?", "container-title": "The New York Times",117                     "URL": "https://www.nytimes.com/2019/03/22/health/memory-forgetting-psychology.html"])118        #expect(formatted("apa", e) == "Carey, B. (2019, March 22). Can we get better at forgetting? *The New York Times*. https://www.nytimes.com/2019/03/22/health/memory-forgetting-psychology.html")119    }120121    @Test func twentyOneOrMoreAuthors() {122        // Rule: apastyle.apa.org "elements of reference list entries": the first 19 authors, a spaced ellipsis ". . .", then the last author.123        // The metadata is the 22-author Kalnay et al. (1996) paper used as the example in the printed Publication Manual; the printed124        // string could not be checked online, so this test asserts the rule, not a quoted example.125        let names = ["Kalnay E.", "Kanamitsu M.", "Kistler R.", "Collins W.", "Deaven D.", "Gandin L.", "Iredell M.", "Saha S.", "White G.", "Woollen J.", "Zhu Y.",126                     "Chelliah M.", "Ebisuzaki W.", "Higgins W.", "Janowiak J.", "Mo K. C.", "Ropelewski C.", "Wang J.", "Leetmaa A.", "Reynolds R.", "Jenne R.", "Joseph D."]127        let authors = names.map { n -> PersonName in let p = n.split(separator: " ", maxSplits: 1); return person(String(p[0]), String(p[1])) }128        let e = BibEntry(key: "kalnay1996", type: "article-journal", authors: authors, year: 1996,129            fields: ["title": "The NCEP/NCAR 40-year reanalysis project", "container-title": "Bulletin of the American Meteorological Society",130                     "volume": "77", "issue": "3", "page": "437–471"])131        #expect(formatted("apa", e) == "Kalnay, E., Kanamitsu, M., Kistler, R., Collins, W., Deaven, D., Gandin, L., Iredell, M., Saha, S., White, G., Woollen, J., Zhu, Y., Chelliah, M., Ebisuzaki, W., Higgins, W., Janowiak, J., Mo, K. C., Ropelewski, C., Wang, J., Leetmaa, A., . . . Joseph, D. (1996). The NCEP/NCAR 40-year reanalysis project. *Bulletin of the American Meteorological Society*, *77*(3), 437–471.")132        // Exactly 20 authors are all listed.133        var twenty = e; twenty.authors = Array(authors.prefix(20))134        #expect(formatted("apa", twenty).contains("Leetmaa, A., & Reynolds, R. (1996)"))135    }136137    @Test func inTextCitations() {138        // In-text forms printed next to each example on the pages above.139        let kesharwani = BibEntry(key: "kesharwani2020", type: "book", editors: [person("Kesharwani", "P.")], year: 2020, fields: ["title": "Nanotechnology"])140        let nci = BibEntry(key: "nci2019", type: "report", authors: [org("National Cancer Institute")], year: 2019, fields: ["title": "Taking time"])141        let census = BibEntry(key: "census", type: "webpage", authors: [org("U.S. Census Bureau")], fields: ["title": "U.S. and world population clock"])142        let salas = BibEntry(key: "salas2020", type: "article-journal", authors: [person("Salas", "E."), person("D’Agostino", "T.")], year: 2020, fields: ["title": "T"])143        let c = Cited(style: "apa", entries: [Self.grady, Self.jackson, kesharwani, nci, census, salas],144                      citations: [[cite("grady2019")], [cite("jackson2019")], [cite("kesharwani2020")], [cite("nci2019")], [cite("census")], [cite("salas2020")]])145        #expect(c.text(cite("grady2019")) == "(Grady et al., 2019)")146        #expect(c.text(cite("jackson2019")) == "(Jackson, 2019)")147        #expect(c.text(cite("kesharwani2020")) == "(Kesharwani, 2020)")148        #expect(c.text(cite("nci2019")) == "(National Cancer Institute, 2019)")149        #expect(c.text(cite("census")) == "(U.S. Census Bureau, n.d.)")150        #expect(c.text(cite("salas2020")) == "(Salas & D’Agostino, 2020)")151        // Locators, prefix, suffix, author suppression; several works are ordered as in the reference list.152        #expect(c.text(cite("jackson2019", "25")) == "(Jackson, 2019, p. 25)")153        #expect(c.text(cite("jackson2019", "pp. 25-27")) == "(Jackson, 2019, pp. 25–27)")154        #expect(c.text(cite("jackson2019", "Chapter 3")) == "(Jackson, 2019, Chapter 3)")155        #expect(c.text(cite("jackson2019", "25", suppressAuthor: true)) == "(2019, p. 25)")156        #expect(c.text(cite("jackson2019", prefix: "see", suffix: "for a review")) == "(see Jackson, 2019 for a review)")157        #expect(c.text(["salas2020", "jackson2019", "grady2019"]) == "(Grady et al., 2019; Jackson, 2019; Salas & D’Agostino, 2020)")158    }159160    @Test func sameAuthorSameYear() {161        // https://apastyle.apa.org/style-grammar-guidelines/citations/basic-principles/same-year-author shows "(Dreschke, n.d.-a, n.d.-b)"162        // and the "2018a" / "2018b" forms. The Judge & Kammeyer-Mueller pair is the printed manual's example (not checked online).163        let j1 = BibEntry(key: "judgeA", type: "article-journal", authors: [person("Judge", "T. A."), person("Kammeyer-Mueller", "J. D.")], year: 2012,164            fields: ["title": "General and specific measures in organizational behavior research: Considerations, examples, and recommendations for researchers",165                     "container-title": "Journal of Organizational Behavior", "volume": "33", "issue": "2", "page": "161–174", "DOI": "10.1002/job.764"])166        let j2 = BibEntry(key: "judgeB", type: "article-journal", authors: [person("Judge", "T. A."), person("Kammeyer-Mueller", "J. D.")], year: 2012,167            fields: ["title": "On the value of aiming high: The causes and consequences of ambition", "container-title": "Journal of Applied Psychology",168                     "volume": "97", "issue": "4", "page": "758–775", "DOI": "10.1037/a0028084"])169        let d1 = BibEntry(key: "d1", type: "webpage", authors: [person("Dreschke", "A.")], fields: ["title": "Alpha notes"])170        let d2 = BibEntry(key: "d2", type: "webpage", authors: [person("Dreschke", "A.")], fields: ["title": "Beta notes"])171        // Cited in the opposite order: the letters follow the reference-list order (by title), not the order of citation.172        let c = Cited(style: "apa", entries: [j2, j1, d2, d1], citations: [[cite("judgeB")], [cite("judgeA")], [cite("d2"), cite("d1")]])173        #expect(c.text(cite("judgeA")) == "(Judge & Kammeyer-Mueller, 2012a)")174        #expect(c.text(cite("judgeB")) == "(Judge & Kammeyer-Mueller, 2012b)")175        #expect(c.text(["judgeB", "judgeA"]) == "(Judge & Kammeyer-Mueller, 2012a, 2012b)")176        #expect(c.text(["d2", "d1"]) == "(Dreschke, n.d.-a, n.d.-b)")177        let bib = c.bibliography178        #expect(bib.count == 4)179        #expect(bib[0].hasPrefix("Dreschke, A. (n.d.-a). *Alpha notes*."))180        #expect(bib[2] == "Judge, T. A., & Kammeyer-Mueller, J. D. (2012a). General and specific measures in organizational behavior research: Considerations, examples, and recommendations for researchers. *Journal of Organizational Behavior*, *33*(2), 161–174. https://doi.org/10.1002/job.764")181        #expect(bib[3].hasPrefix("Judge, T. A., & Kammeyer-Mueller, J. D. (2012b). On the value of aiming high"))182    }183184    @Test func noAuthorAndSorting() {185        // Rules of the Publication Manual (sections 9.12 and 9.44–9.48), asserted as rules: a work without author is entered and186        // alphabetised by its title; one-author entries precede multi-author entries that start with the same name; "nothing precedes something".187        let anon = BibEntry(key: "anon", type: "book", year: 2020, fields: ["title": "The business of wellness", "publisher": "Example Press"])188        let brown = BibEntry(key: "brown", type: "book", authors: [person("Brown", "J. R.")], year: 2019, fields: ["title": "Z"])189        let browning = BibEntry(key: "browning", type: "book", authors: [person("Browning", "A. R.")], year: 2001, fields: ["title": "A"])190        let brownSmith = BibEntry(key: "brownsmith", type: "book", authors: [person("Brown", "J. R."), person("Smith", "A.")], year: 2001, fields: ["title": "B"])191        let c = Cited(style: "apa", entries: [browning, brownSmith, brown, anon], citations: [[cite("browning"), cite("brownsmith"), cite("brown"), cite("anon")]])192        #expect(c.bibliography.map { String($0.prefix(22)) } == ["Brown, J. R. (2019). *", "Brown, J. R., & Smith,", "Browning, A. R. (2001)", "*The business of welln"])193        #expect(c.bibliography[3] == "*The business of wellness*. (2020). Example Press.")194        #expect(c.text(cite("anon")) == "(The business of wellness, 2020)")195    }196}197
Files main
Menu

Theme