Skip to content

Commit

Permalink
Deploying to gh-pages from @ f542662 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd committed Feb 12, 2024
1 parent af4cd31 commit 650acfd
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 28 deletions.
6 changes: 3 additions & 3 deletions _sources/codergpt.test_writer.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ codergpt.test\_writer package
Submodules
----------

codergpt.test\_writer.tester module
-----------------------------------
codergpt.test\_writer.test\_writer module
-----------------------------------------

.. automodule:: codergpt.test_writer.tester
.. automodule:: codergpt.test_writer.test_writer
:members:
:undoc-members:
:show-inheritance:
Expand Down
46 changes: 46 additions & 0 deletions _sources/description.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,52 @@ Commands
By using these optimizations, we improve the efficiency and readability of the code.
"""
5s. **write-tests**: Generates test cases for specified functions and/or classes within a Python code file.

.. code-block:: shell
code write-tests <path> [--function <function_name>] [--class <classname>]
**Example**

- Let's consider a Python file `example.py`:

.. code-block:: python
# example.py
def add(a, b):
return a + b
class Calculator:
def subtract(self, a, b):
return a - b
.. code-block:: shell
$ code write-tests example.py --function add --class Calculator
results in the creation of test files that contain test cases for both the `add` function and the `Calculator` class. The content of the generated test files might look like this:

.. code-block:: python
import unittest
from example import add, Calculator
class TestAddFunction(unittest.TestCase):
def test_addition(self):
self.assertEqual(add(3, 4), 7)
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calc = Calculator()
def test_subtract(self):
self.assertEqual(self.calc.subtract(10, 5), 5)
In this example, executing the command generates unit tests for the `add` function and the `Calculator` class defined in `example.py`. The tests verify whether the `add` function correctly computes the sum of two numbers and if the `Calculator`'s `subtract` method accurately performs subtraction.

Development
-----------
Expand Down
30 changes: 30 additions & 0 deletions _sources/test.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.. py:module:: codergpt
Test writing module
===================

.. py:class:: CodeTester(chain)
The CodeTester class is responsible for generating testing code from a given source file. It utilizes a llm chain to produce tests for specific functions or classes within the source file.

.. py:method:: __init__(chain)
Initializes the CodeTester instance with a provided llm chain.

:param chain: A RunnableSerializable object capable of executing tasks.
:type chain: RunnableSerializable[Dict, Any]

.. py:method:: write_tests(filename, function=None, classname=None, outfile=None)
Generates test cases for the specified code by invoking the llm chain. If a function or class name is provided, it will generate tests specifically for that function or class. Otherwise, it will attempt to create tests for the entire code.

:param filename: The path to the code file for which tests are to be written.
:type filename: Union[str, Path]
:param function: The name of the function for which tests should be generated. Defaults to None, indicating that no specific function is targeted.
:type function: Optional[str]
:param classname: The name of the class for which tests should be generated. Defaults to None, indicating that no specific class is targeted.
:type classname: Optional[str]
:param outfile: The path where the generated test file should be saved. If not provided, a default path within the TEST_DIR will be used.
:type outfile: Optional[str]

The method reads the source code from the provided filename and uses the llm chain to generate appropriate test cases. The resulting test code is then written to either the specified outfile or a new file within the TEST_DIR directory.
20 changes: 13 additions & 7 deletions codergpt.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,13 @@ <h2>Subpackages<a class="headerlink" href="#subpackages" title="Link to this hea
</li>
<li class="toctree-l1"><a class="reference internal" href="codergpt.test_writer.html">codergpt.test_writer package</a><ul>
<li class="toctree-l2"><a class="reference internal" href="codergpt.test_writer.html#submodules">Submodules</a></li>
<li class="toctree-l2"><a class="reference internal" href="codergpt.test_writer.html#module-codergpt.test_writer.tester">codergpt.test_writer.tester module</a></li>
<li class="toctree-l2"><a class="reference internal" href="codergpt.test_writer.html#module-codergpt.test_writer.test_writer">codergpt.test_writer.test_writer module</a><ul>
<li class="toctree-l3"><a class="reference internal" href="codergpt.test_writer.html#codergpt.test_writer.test_writer.CodeTester"><code class="docutils literal notranslate"><span class="pre">CodeTester</span></code></a><ul>
<li class="toctree-l4"><a class="reference internal" href="codergpt.test_writer.html#codergpt.test_writer.test_writer.CodeTester.write_tests"><code class="docutils literal notranslate"><span class="pre">CodeTester.write_tests()</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="codergpt.test_writer.html#module-codergpt.test_writer">Module contents</a></li>
</ul>
</li>
Expand Down Expand Up @@ -209,7 +215,7 @@ <h2>codergpt.cli module<a class="headerlink" href="#codergpt-cli-module" title="
<p>Main python file.</p>
<dl class="py class">
<dt class="sig sig-object py" id="codergpt.main.CoderGPT">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codergpt.main.</span></span><span class="sig-name descname"><span class="pre">CoderGPT</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">model</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'gpt-3.5-turbo'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.main.CoderGPT" title="Link to this definition"></a></dt>
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codergpt.main.</span></span><span class="sig-name descname"><span class="pre">CoderGPT</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">model</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'gpt-4-turbo-preview'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.main.CoderGPT" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>CoderGPT class.</p>
<dl class="py method">
Expand Down Expand Up @@ -285,8 +291,8 @@ <h2>codergpt.cli module<a class="headerlink" href="#codergpt-cli-module" title="
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="codergpt.main.CoderGPT.tester">
<span class="sig-name descname"><span class="pre">tester</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.main.CoderGPT.tester" title="Link to this definition"></a></dt>
<dt class="sig sig-object py" id="codergpt.main.CoderGPT.test_writer">
<span class="sig-name descname"><span class="pre">test_writer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">classname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.main.CoderGPT.test_writer" title="Link to this definition"></a></dt>
<dd><p>Test the code file.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
Expand Down Expand Up @@ -369,7 +375,7 @@ <h2>codergpt.cli module<a class="headerlink" href="#codergpt-cli-module" title="

<dl class="py class">
<dt class="sig sig-object py" id="codergpt.CoderGPT">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codergpt.</span></span><span class="sig-name descname"><span class="pre">CoderGPT</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">model</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'gpt-3.5-turbo'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.CoderGPT" title="Link to this definition"></a></dt>
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codergpt.</span></span><span class="sig-name descname"><span class="pre">CoderGPT</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">model</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'gpt-4-turbo-preview'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.CoderGPT" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>CoderGPT class.</p>
<dl class="py method">
Expand Down Expand Up @@ -445,8 +451,8 @@ <h2>codergpt.cli module<a class="headerlink" href="#codergpt-cli-module" title="
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="codergpt.CoderGPT.tester">
<span class="sig-name descname"><span class="pre">tester</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.CoderGPT.tester" title="Link to this definition"></a></dt>
<dt class="sig sig-object py" id="codergpt.CoderGPT.test_writer">
<span class="sig-name descname"><span class="pre">test_writer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">classname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.CoderGPT.test_writer" title="Link to this definition"></a></dt>
<dd><p>Test the code file.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
Expand Down
26 changes: 24 additions & 2 deletions codergpt.test_writer.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,31 @@ <h1>codergpt.test_writer package<a class="headerlink" href="#codergpt-test-write
<section id="submodules">
<h2>Submodules<a class="headerlink" href="#submodules" title="Link to this heading"></a></h2>
</section>
<section id="module-codergpt.test_writer.tester">
<span id="codergpt-test-writer-tester-module"></span><h2>codergpt.test_writer.tester module<a class="headerlink" href="#module-codergpt.test_writer.tester" title="Link to this heading"></a></h2>
<section id="module-codergpt.test_writer.test_writer">
<span id="codergpt-test-writer-test-writer-module"></span><h2>codergpt.test_writer.test_writer module<a class="headerlink" href="#module-codergpt.test_writer.test_writer" title="Link to this heading"></a></h2>
<p>Test writing module.</p>
<dl class="py class">
<dt class="sig sig-object py" id="codergpt.test_writer.test_writer.CodeTester">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codergpt.test_writer.test_writer.</span></span><span class="sig-name descname"><span class="pre">CodeTester</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chain</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.test_writer.test_writer.CodeTester" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Code tester class writes testing code from a given file.</p>
<dl class="py method">
<dt class="sig sig-object py" id="codergpt.test_writer.test_writer.CodeTester.write_tests">
<span class="sig-name descname"><span class="pre">write_tests</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">classname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">outfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codergpt.test_writer.test_writer.CodeTester.write_tests" title="Link to this definition"></a></dt>
<dd><p>Write tests for the code by invoking the runnable chain.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>path</strong> – The path to the code file to be explained.</p></li>
<li><p><strong>function</strong> (<span class="sphinx_autodoc_typehints-type"><code class="xref py py-data docutils literal notranslate"><span class="pre">Optional</span></code>[<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>]</span>) – The name of the function to explain. Default is None.</p></li>
<li><p><strong>classname</strong> (<span class="sphinx_autodoc_typehints-type"><code class="xref py py-data docutils literal notranslate"><span class="pre">Optional</span></code>[<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>]</span>) – The name of the class to explain. Default is None.</p></li>
</ul>
</dd>
</dl>
</dd></dl>

</dd></dl>

</section>
<section id="module-codergpt.test_writer">
<span id="module-contents"></span><h2>Module contents<a class="headerlink" href="#module-codergpt.test_writer" title="Link to this heading"></a></h2>
Expand Down
43 changes: 43 additions & 0 deletions description.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,49 @@ <h3>Commands<a class="headerlink" href="#commands" title="Link to this heading">
</div>
</li>
</ol>
<p>5s. <strong>write-tests</strong>: Generates test cases for specified functions and/or classes within a Python code file.</p>
<blockquote>
<div><div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>code<span class="w"> </span>write-tests<span class="w"> </span>&lt;path&gt;<span class="w"> </span><span class="o">[</span>--function<span class="w"> </span>&lt;function_name&gt;<span class="o">]</span><span class="w"> </span><span class="o">[</span>--class<span class="w"> </span>&lt;classname&gt;<span class="o">]</span>
</pre></div>
</div>
<p><strong>Example</strong></p>
<ul>
<li><p>Let’s consider a Python file <cite>example.py</cite>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># example.py</span>

<span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>

<span class="k">class</span> <span class="nc">Calculator</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">subtract</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span>
</pre></div>
</div>
</li>
</ul>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>code<span class="w"> </span>write-tests<span class="w"> </span>example.py<span class="w"> </span>--function<span class="w"> </span>add<span class="w"> </span>--class<span class="w"> </span>Calculator
</pre></div>
</div>
<p>results in the creation of test files that contain test cases for both the <cite>add</cite> function and the <cite>Calculator</cite> class. The content of the generated test files might look like this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
<span class="kn">from</span> <span class="nn">example</span> <span class="kn">import</span> <span class="n">add</span><span class="p">,</span> <span class="n">Calculator</span>

<span class="k">class</span> <span class="nc">TestAddFunction</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

<span class="k">def</span> <span class="nf">test_addition</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="mi">7</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">TestCalculator</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

<span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">calc</span> <span class="o">=</span> <span class="n">Calculator</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">test_subtract</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calc</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, executing the command generates unit tests for the <cite>add</cite> function and the <cite>Calculator</cite> class defined in <cite>example.py</cite>. The tests verify whether the <cite>add</cite> function correctly computes the sum of two numbers and if the <cite>Calculator</cite>’s <cite>subtract</cite> method accurately performs subtraction.</p>
</div></blockquote>
</section>
</section>
<section id="development">
Expand Down
Loading

0 comments on commit 650acfd

Please sign in to comment.