<commit_before>(defpackage :unet-logging-test
  (:use :cl)
  
  (:import-from :unet-logging
                :make-logger
                :add-logger-category
                :start-logging
                :stop-logging
                :log-string))

(in-package :unet-logging-test)

;;; Make sure all of the expected API points exist
(nst:def-test-group existence-tests ()
  (nst:def-test logging-package-exists (:true)
    (find-package :unet-logging))
  
  (nst:def-test logging-functions-exist (:each (:function-ish))
    '(unet-logging:make-logger
      unet-logging:add-logger-category
      unet-logging:start-logging
      unet-logging:stop-logging
      unet-logging:log-string)))

;;; Make sure we can prepare a logger
(nst:def-fixtures simple-logger
    (:documentation "Defines a logger LOGGER.")
  (logger (make-logger)))

(nst:def-test-group instantiate-logger (simple-logger)
  (nst:def-test make-logger-succeeds (:true)
    logger)
  
  (nst:def-test add-base-category-succeeds (:eq :animal)
    (add-logger-category logger :animal))

  (nst:def-test add-sub-category-succeeds (:eq :animal)
    (add-logger-category logger :animal :mammal))
  
  (nst:def-test add-multiple-sub-categories-succeeds (:eq :mammal)
    (add-logger-category logger :mammal :dog :cat :mouse :human)))

;;; Make sure we can log a simple message
(nst:def-fixtures categoried-logger
    (:documentation "Defines a logger LOGGER with some basic categories."
     :special (logger)
     :setup (progn
              (add-logger-category logger :fish)
              (add-logger-category logger :primate :ape :chimp :human)
              (add-logger-category logger :mammal :dog :cat :mouse :primate)
              (add-logger-category logger :animal :mammal :fish))))

(defmacro with-logging-to-string ((logger &rest categories) &body body)
  (let ((handle (gensym "LOGGER-HANDLE-"))
        (stream (gensym "STREAM-")))
    `(with-output-to-string (,stream)
       (start-logging ,logger ,stream ',handle ,@categories)
       (unwind-protect
            (progn ,@body)
         (stop-logging ,logger ',handle)))))

(nst:def-test-group log-simple-messages (simple-logger categoried-logger)
  (nst:def-test log-a-message-to-nowhere (:not :err)
    (log-string logger :dog "Woof"))
  
  (nst:def-test log-message-works (:regex ".*DOG Woof$")
    (with-logging-to-string (logger)
      (log-string logger :dog "Woof")))
  
  (nst:def-test log-message-for-subcategory (:regex ".*DOG Woof$")
    (with-logging-to-string (logger :mammal)
      (log-string logger :dog "Woof")))
  
  (nst:def-test log-message-unclaimed (:equal "")
    (with-logging-to-string (logger :mammal)
      (log-string logger :fish "Bloop")))
  
  (nst:def-test log-messages-to-different-logs
      (:values (:regex ".*CAT Meow.*DOG Woof$") (:regex ".*FISH Bloop$"))
    (let (m f)
      (setf m (with-logging-to-string (logger :mammal)
                (setf f (with-logging-to-string (logger :fish)
                          (log-string logger :cat "Meow")
                          (log-string logger :dog "Woof")
                          (log-string logger :fish "Bloop")))))
      (values m f))))
<commit_msg>Took advantage of the use-package declaration<commit_after>(defpackage :unet-logging-test
  (:use :cl)
  
  (:import-from :unet-logging
                :make-logger
                :add-logger-category
                :start-logging
                :stop-logging
                :log-string))

(in-package :unet-logging-test)

;;; Make sure all of the expected API points exist
(nst:def-test-group existence-tests ()
  (nst:def-test logging-package-exists (:true)
    (find-package :unet-logging))
  
  (nst:def-test logging-functions-exist (:each (:function-ish))
    '(make-logger
      add-logger-category
      start-logging
      stop-logging
      log-string)))

;;; Make sure we can prepare a logger
(nst:def-fixtures simple-logger
    (:documentation "Defines a logger LOGGER.")
  (logger (make-logger)))

(nst:def-test-group instantiate-logger (simple-logger)
  (nst:def-test make-logger-succeeds (:true)
    logger)
  
  (nst:def-test add-base-category-succeeds (:eq :animal)
    (add-logger-category logger :animal))

  (nst:def-test add-sub-category-succeeds (:eq :animal)
    (add-logger-category logger :animal :mammal))
  
  (nst:def-test add-multiple-sub-categories-succeeds (:eq :mammal)
    (add-logger-category logger :mammal :dog :cat :mouse :human)))

;;; Make sure we can log a simple message
(nst:def-fixtures categoried-logger
    (:documentation "Defines a logger LOGGER with some basic categories."
     :special (logger)
     :setup (progn
              (add-logger-category logger :fish)
              (add-logger-category logger :primate :ape :chimp :human)
              (add-logger-category logger :mammal :dog :cat :mouse :primate)
              (add-logger-category logger :animal :mammal :fish))))

(defmacro with-logging-to-string ((logger &rest categories) &body body)
  (let ((handle (gensym "LOGGER-HANDLE-"))
        (stream (gensym "STREAM-")))
    `(with-output-to-string (,stream)
       (start-logging ,logger ,stream ',handle ,@categories)
       (unwind-protect
            (progn ,@body)
         (stop-logging ,logger ',handle)))))

(nst:def-test-group log-simple-messages (simple-logger categoried-logger)
  (nst:def-test log-a-message-to-nowhere (:not :err)
    (log-string logger :dog "Woof"))
  
  (nst:def-test log-message-works (:regex ".*DOG Woof$")
    (with-logging-to-string (logger)
      (log-string logger :dog "Woof")))
  
  (nst:def-test log-message-for-subcategory (:regex ".*DOG Woof$")
    (with-logging-to-string (logger :mammal)
      (log-string logger :dog "Woof")))
  
  (nst:def-test log-message-unclaimed (:equal "")
    (with-logging-to-string (logger :mammal)
      (log-string logger :fish "Bloop")))
  
  (nst:def-test log-messages-to-different-logs
      (:values (:regex ".*CAT Meow.*DOG Woof$") (:regex ".*FISH Bloop$"))
    (let (m f)
      (setf m (with-logging-to-string (logger :mammal)
                (setf f (with-logging-to-string (logger :fish)
                          (log-string logger :cat "Meow")
                          (log-string logger :dog "Woof")
                          (log-string logger :fish "Bloop")))))
      (values m f))))

{'fraction_non_alphanumeric': 0.14961129620815486, 'fraction_numerical': 0.0, 'mean_word_length': 2.708235294117647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5750724', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2067, 'n_words': 487}